需要个jar包:commons-net-3.3-bin.zip
下载地址:http://commons.apache.org/proper/commons-net/download_net.cgi
你找的代码我修改过了,复制直接使用。linux系统需要先开启telnet服务
//执行的命令
System.out.println(she.sendCommand("ll"));
这个输入linux 命令
import java.io.InputStream;
import java.io.PrintStream;
import 
.org.apache.commons.net.telnet.TelnetClient;
public class Shell {
private TelnetClient telnet = new TelnetClient();
private InputStream in;
private PrintStream out;
private char prompt = '$';// 普通用户结束
public Shell(String ip, int port, String user, String password) {
try {
telnet.connect(ip, port);
in = telnet.getInputStream();
out = new PrintStream(telnet.getOutputStream());
// 根据root用户设置结束符
this.prompt = user.equals("root") ? '#' : '>';
login(user, password);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 登录
*
* @param user
* @param password
*/
public void login(String user, String password) {
//
read()()Until("login:");
readUntil("login:");
write(user);
readUntil("Password:");
write(password);
readUntil(prompt + "");
}
/**
* 读取分析结果
*
* @param pattern
* @return
*/
public String readUntil(String pattern) {
try {
char lastChar = pattern.charAt(pattern.length() - 1);
StringBuffer sb = new StringBuffer();
char ch = (char) in.read();
while (true) {
sb.append(ch);
if (ch == lastChar) {
if (sb.toString().endsWith(pattern)) {
return sb.toString();
}
}
ch = (char) in.read();
System.out.print(ch);
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 写操作
*
* @param value
*/
public void write(String value) {
try {
out.println(value);
out.flush();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 向目标发送命令
字符串 *
* @param command
* @return
*/
public String sendCommand(String command) {
try {
write(command);
return readUntil(prompt + "");
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 关闭连接
*/
public void disconnect() {
try {
telnet.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
// TelnetClient telnet = new TelnetClient();
try {
Shell she = new Shell("192.168.1.10", 23, "root", "123456");
System.out.println(she);
//执行的命令
System.out.println(she.sendCommand("ll"));
she.disconnect();
} catch (Exception e) {
// TODO: handle exception
}
}
}
追问我要的是windows的代码。。。linux的用不了啊。
追答代码太长没法贴了
追问可以运行!但有乱码
![](https://video.ask-data.xyz/img.php?b=https://iknow-pic.cdn.bcebos.com/bd3eb13533fa828b7a8e7ac7ff1f4134970a5a61?x-bce-process=image%2Fresize%2Cm_lfit%2Cw_600%2Ch_800%2Climit_1%2Fquality%2Cq_85%2Fformat%2Cf_auto)
后来我在最后的打印结果System.out.println(str)
![](https://video.ask-data.xyz/img.php?b=https://iknow-pic.cdn.bcebos.com/5ab5c9ea15ce36d35604621438f33a87e950b196?x-bce-process=image%2Fresize%2Cm_lfit%2Cw_600%2Ch_800%2Climit_1%2Fquality%2Cq_85%2Fformat%2Cf_auto)
;前面加了以下几句转换编码的
出中文了,但还有乱码
![](https://video.ask-data.xyz/img.php?b=https://iknow-pic.cdn.bcebos.com/b999a9014c086e06570ad96f00087bf40ad1cb70?x-bce-process=image%2Fresize%2Cm_lfit%2Cw_600%2Ch_800%2Climit_1%2Fquality%2Cq_85%2Fformat%2Cf_auto)
追答乱码你就自己解决吧。
追问知道问题所在了,除了上一步要进行编码转换之外还要在开头加个参数
把开头的private TelnetClient telnet = new TelnetClient();
改成private TelnetClient telnet = new TelnetClient("VT220");
幸苦你了,给你满意答案