使用JSch在远程SSH会话上运行telnet命令
我正在研究在SSH shell会话上运行telnet
命令,为了获得它,我按照官方示例使用了JSch。
我在StackOverflow上也按照这个例子编写了我自己的代码
这是我的代码:
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
你能帮我找到问题吗?!
我知道了。
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; } }
注意,shell是一个Linux shell。 在windows中,“exit”命令应该是不同的
- 当代码已知时,HttpURLConnection.getResponseCode()会抛出IOException
- 关闭JasperViewer时如何防止关闭JFrame
- 在Spring 5 Webflux中启用CORS?
- 在web.xml中声明JSP taglib指令
- java.lang.IllegalStateException:前面的方法调用getMessage(“title”)缺少行为定义
- 用于反转字节顺序/更改字节顺序的命令行
- sun-jaxws.xml中的JAX-WS多端点
- Collectors.summingInt()vs mapToInt()。sum()
- Java Security&BouncyCastle API让人感到困惑和困惑