I am trying to use jsch to connect to a remote switch and run some command and extract the output.
I am able to connect to the switch using , however the command output
Please try the code below - tested and working
Channel channel = session.openChannel("shell");
OutputStream ops = channel.getOutputStream();
PrintStream ps = new PrintStream(ops);
channel.connect();
ps.println("pwd");
ps.println("exit");
ps.flush();
ps.close();
InputStream in = channel.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
System.out.println("Opening...");
String jarOutput;
while ((jarOutput = reader.readLine()) != null)
System.out.println(jarOutput);
reader.close();
channel.disconnect();
Output -
Opening...
user@host:~> pwd
/home/user
user@host:~>
user@host:~> exit
logout