Issue
I have a task: to do telnet commands through ssh session.
I have read similar problems:
- Running telnet command on remote SSH session using JSch
- telnet through SSH
- Execution hangs after Running telnet command on remote SSH session using JSch
And I wrote the code (with lib https://mvnrepository.com/artifact/com.jcraft/jsch):
Class Ssh:
public class Ssh { private final String USER; private final String PASSWORD; private final String HOST;
public Ssh(String user, String password, String host) { this.USER = user; this.PASSWORD = password; this.HOST = host; } public Session runSsh() { try { Session session = new JSch().getSession(USER, HOST, 22); session.setPassword(PASSWORD); // It must not be recommended, but if you want to skip host-key check, session.setConfig("StrictHostKeyChecking", "no"); session.connect(3000); return session; } catch (JSchException e) { System.out.println(e); } return null; }
}
Class Telnet
public class Telnet {
public String runCommand(Session session, String command) throws Exception { Channel channel = session.openChannel("shell"); channel.connect(3000); DataOutputStream outputStream = new DataOutputStream(channel.getOutputStream()); outputStream.writeBytes("telnet localhost 5000\r\n"); outputStream.writeBytes(command + "\r\n"); outputStream.writeBytes("exit\r\n"); outputStream.flush(); DataInputStream inputStream = new DataInputStream(channel.getInputStream()); BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); channel.setInputStream(inputStream, true); String line = reader.readLine(); String result = line +"\n"; while (!(line= reader.readLine()).equals("exit")){ result += line +"\n"; } result += "Connection closed by foreign host"; outputStream.close(); inputStream.close(); channel.disconnect(); session.disconnect(); return result; }
}
Class Main
public class Main {
public static void main(String[] arg) { Ssh ssh = new Ssh("user","password","ip-server"); Telnet telnet = new Telnet(); try { Session sessionSsh = ssh.runSsh(); String result = telnet.runCommand(sessionSsh, "H"); System.out.println(result); } catch (Exception e) { e.printStackTrace(); } }
}
I get the result:
telnet localhost 5000
Entering character mode
Escape character is '^]'.
Command Shell 1 on intelpc-1 (Port: 5000)
b01_1_1 (5000) >
H
Connection closed by foreign host
Process finished with exit code 0
I don't see the result of executing the command telnet...
I made different commands via telnet:
- H
or
- u s3
But I can't see their result :(
Tell me please. How do I get the results of the telnet command execution?
Solution
I debugged your example and in my case, I needed to wait for the program, which you connect via telnet, until it is ready to receive a command.
In the second step, you can send your command and the exit command to stop the telnet, so you are reading from the stream until it has stopped by saying "Connection closed...". Be aware, that exiting from telnet can have different commands, some use quit
, some other use exit
or only wait for a termination signal.
class Telnet {
public String runCommand(Session session, String command) throws Exception {
Channel channel = session.openChannel("shell");
channel.connect(3000);
DataOutputStream outputStream = new DataOutputStream(channel.getOutputStream());
outputStream.writeBytes("telnet localhost 16379\r\n");
outputStream.flush();
DataInputStream inputStream = new DataInputStream(channel.getInputStream());
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
channel.setInputStream(inputStream, true);
// Read until we are ready to write
String line;
while (!(line= reader.readLine()).equals("Escape character is '^]'.")){
System.out.println(line);
}
// write command and exit telnet
outputStream.writeBytes(command + "\r\n");
outputStream.writeBytes("quit\r\n");
outputStream.flush();
// read until telnet has closed
String result = "";
while (!(line= reader.readLine()).equals("Connection closed by foreign host.")){
result += line +"\n";
}
outputStream.close();
inputStream.close();
channel.disconnect();
session.disconnect();
return result;
}
}
In the end, there are alternative ways to communicate without telnet, i.e. local port forwarding.
Answered By - Matthias Wiedemann