【问题标题】:Running telnet command on remote SSH session using JSch使用 JSch 在远程 SSH 会话上运行 telnet 命令
【发布时间】:2015-01-24 15:41:02
【问题描述】:

我正在努力在 SSH shell 会话上运行 telnet 命令,为了获得它,我按照官方 example 使用 JSch。

我在 StackOverflow

上也遵循 this example 编写了自己的代码

这是我的代码:

package Utility;

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.InputStreamReader;


public class JavaTelnet {

    public static void main(String[] arg) {
        try {
            System.out.println(telnetConnection(USER_ID,PASSWORD,REMOTE_HOST));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static String telnetConnection(String user, String password, String host) throws JSchException, Exception {
      JSch jsch=new JSch();
      Session session=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();
      //session.connect(30000);   // making a connection with timeout.

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

      channel.connect();

      DataInputStream dataIn = new DataInputStream(channel.getInputStream());
      BufferedReader reader = new BufferedReader(new InputStreamReader(dataIn));
      DataOutputStream dataOut = new DataOutputStream(channel.getOutputStream());

      dataOut.writeBytes("telnet localhost 4242\r\n");
/*
* telnet COMMANDS here
*/
      dataOut.writeBytes("exit\r\n");
       dataOut.writeBytes("logout\r\n");
       dataOut.flush();

      String line = reader.readLine();
      String result = line +"\n";

      while ((line= reader.readLine())!=null){
          result += line +"\n";

      }

      dataIn.close();
      dataOut.close();
      System.out.println("disconnecting....");
      channel.disconnect();
      session.disconnect();

      return "done";   

  }

}

看起来不错,但奇怪的是它只能在调试模式下工作。如果我运行它,程序不会达到他的目的。我认为它正在锁定 while 循环,但我不明白为什么。如果没有 while 循环,代码将到达末尾,但不执行任何 shell 命令。

我正在使用 Netbeans 作为 IDE

你能帮我找出问题吗?!

【问题讨论】:

    标签: java ssh telnet


    【解决方案1】:

    我明白了。

    line 在 while 循环中从不 null

    为什么它在调试中起作用仍然是个谜。

    我正在发布我的新代码。我希望它可以对每个想要制作类似东西的人有所帮助。

    package Utility;
    
    import com.jcraft.jsch.Channel;
    import com.jcraft.jsch.JSch;
    import com.jcraft.jsch.JSchException;
    import com.jcraft.jsch.Session;
    import java.io.BufferedReader;
    import java.io.DataInputStream;
    import java.io.DataOutputStream;
    import java.io.InputStreamReader;
    
    
    public class JavaTelnet {
    
        public static void main(String[] arg) {
            try {
                System.out.println(telnetConnection(YOUR_COMMAND,YOUR_USER,YOUR_PASS,YOUR_HOST));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        public static String telnetConnection(String command, String user, String password, String host) throws JSchException, Exception {
          JSch jsch=new JSch();
          Session session=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);
          //session.connect(30000);   // making a connection with timeout.
    
          Channel channel=session.openChannel("shell");
    
          channel.connect(3000);
    
          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 4242\r\n");
    //      dataOut.writeBytes("enable\r\n");
          dataOut.writeBytes(command+"\r\n");
          dataOut.writeBytes("exit\r\n"); //exit from telnet
           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";
          }
    
          dataIn.close();
          dataOut.close();
          channel.disconnect();
          session.disconnect();
    
          return result;   
    
      }
    
    }
    

    注意 外壳是Linux外壳。在 Windows 中,“退出”命令应该不同

    【讨论】:

    • 只是一个注释。在我的 Mac 上,我必须将 '\r\n' 替换为 '\n',否则会执行失败。它应该是“连接被外国主机关闭”。 (加点)。 :-)
    猜你喜欢
    • 2017-08-13
    • 2011-01-25
    • 1970-01-01
    • 2021-01-29
    • 2018-03-03
    • 2017-03-19
    • 1970-01-01
    • 2012-08-11
    • 1970-01-01
    相关资源
    最近更新 更多