0

I was looking for a solution to take SSH connection followed by Telnet connection to remote system via java. As using telnet connection only I can execute that particular command on remote machine.

After browsing a lot I found this answer "https://stackoverflow.com/questions/27146991/running-telnet-command-on-remote-ssh-session-using-jsch" But after Executing the "telnet localhost 4444" the program executions hangs & never come out from while loop . Because of that I'm not able to execute other commands after taking telnet connection.

My code is :-

    public static void main(String[] arg) {
    try {
        System.out.println(telnetConnection(command, puttyUserName,
    puttyPassword, puttyHostName));
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public static String telnetConnection(String command, String user, String password, String host)
        throws JSchException, Exception {
    JSch jsch = new JSch();
    jsch.addIdentity(puttyPublicKey, puttyPassword);
    Session session = jsch.getSession(user, host, 22);

    session.setConfig("StrictHostKeyChecking", "no");

    session.connect(500);//This timeout is not working as mentioned in the example. Program execution never stops.

    Channel channel = session.openChannel("shell");

    channel.connect(500);

    DataInputStream dataIn = new DataInputStream(channel.getInputStream());
    BufferedReader reader = new BufferedReader(new InputStreamReader(dataIn));
    DataOutputStream dataOut = new DataOutputStream(channel.getOutputStream());
    System.out.println("Starting telnet connection...");
    dataOut.writeBytes("telnet localhost 4444\r\n"); after this no commands executes
    dataOut.writeBytes(command + "\r\n"); 

    dataOut.writeBytes("quit\r\n");

// using quit I'm able to exit from telnet session while doing manually via putty as exit doesn't work

    dataOut.writeBytes("exit\r\n"); // exit from shell
    dataOut.flush();
    String line = reader.readLine(); 
    String result = line + "\n";
    while (!(line = reader.readLine()).equals("Connection closed by foreign host")) 
    {
        result += line + "\n";

        System.out.println("heart beat" + result);
    }

    System.out.println("after while done");
    dataIn.close();
    dataOut.close();
    channel.disconnect();
    session.disconnect();
    System.out.println("done");
    return result;
}

}

output//

heart beat telnet localhost 4444

START TRANSLATOR ABCLOC HIGH

quit

exit

[XYZ]$ telnet localhost 4444 Trying x.x.x.1...

Connected to localhost.localdomain (x.x.x.1).

Escape character is '^]'.

Connection to INTERFACE LAYER heart beat telnet localhost 4444

START TRANSLATOR ABCLOC HIGH

quit

exit

[XYZ]$ telnet localhost 4444 Trying x.x.x.1...

Connected to localhost.localdomain (x.x.x.1).

Escape character is '^]'.

Connection to INTERFACE LAYER Type "help" for a list of commands

////after this programs hangs & no actions are performed

Community
  • 1
  • 1
kapil
  • 1
  • 4

1 Answers1

0

I don't have the same server running on port 4444 as you do, but I got similar behaviour in a local test issuing "telnet localhost 80" and "GET /" within JSch. The fix in my case was to be careful about the exactness of the "Connection closed" message. In my case the server sending:

Connection closed by foreign host.

whereas your code is checking for

Connection closed by foreign host

(no full stop) and so that loop never terminates. You can find my test code at https://github.com/pgleghorn/JSchTest

  • Thanks @Phil - But I also tried with the same Connection closed message which you have mentioned. The result was same. – kapil Mar 31 '17 at 18:55
  • The main problem is after executing **Telnet localhost 4444** it is not allowing me to send/execute any other command... I found that this kind of issue is open @many sites... for ex- [link] (http://android.stackexchange.com/questions/161461/using-android-emulators-root-shell-via-command-line) – kapil Mar 31 '17 at 18:58