Reputation: 8011
When I connect to SSH using PuTTY, I get the following message as given below.
Welcome to Ubuntu 16.04.1 LTS (GNU/Linux 4.4.0-98-generic x86_64)
* Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/advantage
deb@ubuntu:~$
Similarly, when I use JSch library to connect, I should also get the same message in Java. Please help me about how to get this. I provide below my code details.
public String connect() {
String errorMessage = null;
try {
sesConnection = jschSSHChannel.getSession(userName, ipOrHostName, PORT);
sesConnection.setPassword(password);
sesConnection.setConfig("StrictHostKeyChecking", "no");
sesConnection.connect(TIMEOUT);
//After connecting, I should get the welcome message
} catch (JSchException jschX) {
errorMessage = jschX.getMessage();
}
return errorMessage;
}
I want to get the welcome message from a unix system before executing the commands in JSch. Please help about how to get it.
Upvotes: 1
Views: 1469
Reputation: 202292
The message you are seeing is simply printed on a shell output.
That does not go well with automatic its reading and executing commands.
I general to automate a command execution, you correctly plan to use SSH channel "exec" (ChannelExec
in JSch). But that channel won't give you the message.
You would have to use "shell" channel (ChannelShell
in JSch). But that channel is intended for implementing an interactive shell session. Not to automate command execution and not for parsing command output. Particularly in the context of your question, there's no realiable way to find out the end of the "welcome message". You would have to read the output line by line, until you get the prompt ("deb@ubuntu:~$"
).
Or, if your use case allows that, you can use ChannelShell
to get the message and then use ChannelExec
to execute the commands.
You better talk to the server administrator to make the information you need available using a different API (like using SSH2_MSG_USERAUTH_BANNER
, or even other way).
See my answer to SSH MOTD per user to understand the different ways how the server can provide the "welcome message". Had the server used SSH2_MSG_USERAUTH_BANNER
, you could use UserInfo.showMessage
to capture the message. See also How to read the SSH key-sig pair banner (for generating SSH password) after connecting to host in Java?
Upvotes: 2