I have code to connect to a remote server via ssh and send 2 or more commands to it (for example: cd /export/home/ops/bin
and "./viewlinkload –time 20131205-19") but I don't see the command executed and don't receive results.
I need to get the result returned from server and display it.
This is code send command:
try {
command = "cd /export/home/ops/bin";
command1="./viewlinkload –time 20131205-19";
session.startShell();
session.getOutputStream().write(command.getBytes());
ChannelInputStream in = session.getInputStream();
ChannelOutputStream out = session.getOutputStream();
InputStream inputStream = session.getInputStream();
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(inputStream));
StringBuilder stringBuilder = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
stringBuilder.append(line);
stringBuilder.append('\n');
}
System.out.println("ke qua" + stringBuilder.toString());
// return stringBuilder.toString();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
If i Change command is "ls\n" After the last record is suspend at "while ((line = bufferedReader.readLine()) != null)" and don't run. Help me. Thanks all.
Jsch has some excellent examples in the examples directory, the one in particular you might find of interest is called
Exec
. You might also be interested inShell
This is a slightly modified version which skips getting the information from the command line and prompting for the user info and command and simply attempts to connect directly to the remote machine and execute a
ls
command.I tested this on my Windows box connecting to one of my Mac boxes without any issues
Updated with hacked Shell example
Basically, this is a hacked example based on the
Shell
example.This uses a custom
OutputStream
to monitor changes to the content being sent from the remote machine and which can issue commands. This is pretty basic, in the fact that all I'm doing is waiting for$
to be send to the output stream and then issuing the next command.It wouldn't take too much work to modify it so that, based on the current command/command index, you could do different parsing...