[英]Running telnet command on remote SSH session using JSch
I am working on running telnet
command on SSH shell session, for obtaining it I used JSch following the official example . 我正在SSH外壳会话上运行
telnet
命令,为了获得它,我在官方示例后面使用了JSch。
I wrote my own code following also this example on StackOverflow 我也在StackOverflow上也遵循此示例编写了自己的代码
this is my code: 这是我的代码:
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";
}
}
It look like good but the strange thing is that it is working only in debug mode . 看起来不错,但奇怪的是它仅在调试模式下工作 。 If I run it the program don't reach his end.
如果我运行它,程序将无法执行。 I think it is locking on the while loop but I don't understand why.
我认为它锁定了while循环,但我不明白为什么。 Without the while loop the code reach his end but don't execute any shell command.
没有while循环,代码将到达末尾,但不执行任何shell命令。
I am using Netbeans as IDE 我正在使用Netbeans作为IDE
Can you help me to find the problem?! 您能帮我发现问题吗?
I got it. 我知道了。
line was never null in the while loop. 在while循环中, line永远不会为null 。
It remains the mystery about why it worked in debug. 为何在调试中起作用仍然是个谜。
I am posting my new code . 我正在发布新代码 。 I hope it can be helpful for everyone wants to make something similar.
我希望它对每个想做类似事情的人都有帮助。
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;
}
}
NB the shell is a Linux shell. 注意,该外壳是Linux外壳。 In windows the "exit" command should be different
在Windows中,“退出”命令应该不同
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.