linux下运行telnet问题
用的是apache共享包这段代码在windows下面可以正常connect远程主机,并且可以执行命令返回结果,disconnect
但是在LinuxCentOS下面测试发现login的时候可以读取到"login:"之后write指令就无响应了
windows下面返回值乱码问题用iso转gbk解决的
Linux默认字符集是zh_CN.UTF-8
import java.io.*;--------------------编程问答-------------------- getCurrentDir是读取当前路径
import org.apache.commons.net.telnet.*;
public class ApacheTelnet {
public ApacheTelnet() {
telnet = new TelnetClient("VT220");
promptText = prompt;
connetionStatus = false;
}
public boolean Connect(String ip, int port, String user, String password) {
try {
if (connetionStatus == false) {
telnet.connect(ip, port);
in = telnet.getInputStream();
out = new PrintStream(telnet.getOutputStream());
login(user, password);
connetionStatus = true;
return true;
} else {
return false;
}
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
public void login(String user, String password) {
readUntil("login:");
write(user);
readUntil("password:");
write(password);
readUntil(prompt);
}
public void write(String value) {
try {
out.println(value);
out.flush();
Thread.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
}
}
public boolean disconnect() {
try {
if (connetionStatus == true) {
telnet.disconnect();
connetionStatus = false;
return true;
} else {
return false;
}
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
public String readUntil(String pattern) {
try {
StringBuffer sb = new StringBuffer();
// boolean found = false;
char ch = (char) in.read();
sb.append(ch);
while (sb.toString().endsWith(pattern) != true) {
ch = (char) in.read();
sb.append(ch);
}
return sb.toString();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public String sendCommand(String command) {
try {
getCurrentDir();
write(command);
returnText = readUntil(promptText);
returnText = isoToGbk(returnText);
returnText = returnText.substring(command.length() + 2,
returnText.length() - promptText.length());
return returnText;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public void getCurrentDir() {
try {
write(" ");
promptText = readUntil(prompt);
promptText = isoToGbk(promptText);
promptText = promptText.substring(3, promptText.length());
} catch (Exception e) {
e.printStackTrace();
}
}
public static String isoToGbk(String para) {
try {
return new String(para.getBytes("ISO-8859-1"), "GBK");
} catch (Exception e) {
return "";
}
}
public static String gbkToIso(String para) {
try {
return new String(para.getBytes("GBK"), "ISO-8859-1");
} catch (Exception e) {
return "";
}
}
private TelnetClient telnet;
private InputStream in;
private PrintStream out;
private String promptText;
private String returnText;
private String prompt = ">";
private boolean connetionStatus;
}
windows的结尾字符串是">"但是会出现返回值如果包含">"就会造成读取部分返回值,所以我用当前路径作为判断返回读完,各位有什么更好的方法判断读取完了?
Linux下面为什么会没有响应,Debug了一下,第一个readUntil("login:"正常读取,卡在第一个write --------------------编程问答-------------------- 把Linux改成zh_CN.GBK也不行啊 --------------------编程问答-------------------- 问题貌似找到,linux下面把write的println改成print("\r\n")就可以正常发送命令,但是有个奇怪,为什么在windows下面反而不行了呢
补充:Java , Java SE