package test;import org.apache.commons.net.telnet.*;
import java.io.*;
public class TmhTelnet {
private static TelnetClient tc = null;
private InputStream in;
private PrintStream out;
public static void main(String args[]) {
new TmhTelnet("192.168.0.100", 23, "user", "密码");
} public TmhTelnet(String host, int port, String username, String password) {
byte[] a = new byte[1024];
if (intconnect(host, port)) {
write(username);
write(password);
writescript("command.txt");//dir
closeconnect(); }
} private void writescript(String filename) {
try {
if (new File(filename).exists()) {
FileReader f = new FileReader(filename);
BufferedReader reader = new BufferedReader(f);
String command = "";
while (command != null) {
command = reader.readLine();
write(command);
}
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} } public void write(String command) {
try {
System.out.println("command:>>>>>>>>>" + command);
out.println(command);
out.flush();
printresponse2();
//for(int i=0;i<800000000;i++);
//茼蜆妏蚚盄最肮祭妏最唗婓諉忳善勤源眈茼綴婬硒俴狟珨沭韜鍔涴爵妏蚚for悜遠懂耀攜勤源腔眈茼奀潔
} catch (Exception e) {
e.printStackTrace();
}
} private boolean intconnect(String host, int port) {
try {
tc = new TelnetClient();
TerminalTypeOptionHandler ttopt = new TerminalTypeOptionHandler(
"vt100", false, false, true, false);
EchoOptionHandler echoopt = new EchoOptionHandler(true, false,
true, false);
SuppressGAOptionHandler gaopt = new SuppressGAOptionHandler(true,
true, true, true); tc.addOptionHandler(ttopt);
tc.addOptionHandler(echoopt);
tc.addOptionHandler(gaopt);
tc.connect(host, port);
in = tc.getInputStream();
out = new PrintStream(tc.getOutputStream());
return true;
} catch (Exception e) {
e.printStackTrace();
try {
tc.disconnect();
in.close();
out.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return false;
}
} private void closeconnect() {
try {
tc.disconnect();
in.close();
//out.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
} private String printresponse() {
try {
byte[] buff = new byte[1024];
int ret_read = 0;
do {
ret_read = in.read(buff);
String a = new String(buff, 0, ret_read);
System.out.print("=" + a);
if (a.endsWith(":") | a.endsWith(">") | a.endsWith("]")) {
System.out.print(a);
return null;
} } while (ret_read >= 0);
} catch (Exception e) {
System.err.println("Exception while reading socket:"
+ e.getMessage());
} try {
tc.disconnect();
} catch (Exception e) {
System.err.println("Exception while closing telnet:"
+ e.getMessage());
}
return null;
} private String printresponse2() {
try {
byte[] buff = new byte[1024];
int ret_read = 0;
ret_read = in.read(buff);
while (ret_read >= 0) {
String a = new String(buff, 0, ret_read);
a = a.trim();
System.out.print("=" + a);
if (a.endsWith(":") | a.endsWith(">") | a.endsWith("]")) {
System.out.print(a);
return null;
}
}
} catch (Exception e) {
System.err.println("Exception while reading socket:"
+ e.getMessage());
} try {
tc.disconnect();
} catch (Exception e) {
System.err.println("Exception while closing telnet:"
+ e.getMessage());
}
return null;
}}返回的是
[5;1H[K[6;1H[K[7;1H[K[8;1H[K[9;1H[K[10;1H[K[11;1H[K[12;1H[K[13;1H[K[14;1H[K[15;1H[K[16;1H[K[17;1H[K[18;1H[K[19;1H[K[20;1H[K
那位高手有解决的方法
我是新手没有多少分
import java.io.*;
public class TmhTelnet {
private static TelnetClient tc = null;
private InputStream in;
private PrintStream out;
public static void main(String args[]) {
new TmhTelnet("192.168.0.100", 23, "user", "密码");
} public TmhTelnet(String host, int port, String username, String password) {
byte[] a = new byte[1024];
if (intconnect(host, port)) {
write(username);
write(password);
writescript("command.txt");//dir
closeconnect(); }
} private void writescript(String filename) {
try {
if (new File(filename).exists()) {
FileReader f = new FileReader(filename);
BufferedReader reader = new BufferedReader(f);
String command = "";
while (command != null) {
command = reader.readLine();
write(command);
}
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} } public void write(String command) {
try {
System.out.println("command:>>>>>>>>>" + command);
out.println(command);
out.flush();
printresponse2();
//for(int i=0;i<800000000;i++);
//茼蜆妏蚚盄最肮祭妏最唗婓諉忳善勤源眈茼綴婬硒俴狟珨沭韜鍔涴爵妏蚚for悜遠懂耀攜勤源腔眈茼奀潔
} catch (Exception e) {
e.printStackTrace();
}
} private boolean intconnect(String host, int port) {
try {
tc = new TelnetClient();
TerminalTypeOptionHandler ttopt = new TerminalTypeOptionHandler(
"vt100", false, false, true, false);
EchoOptionHandler echoopt = new EchoOptionHandler(true, false,
true, false);
SuppressGAOptionHandler gaopt = new SuppressGAOptionHandler(true,
true, true, true); tc.addOptionHandler(ttopt);
tc.addOptionHandler(echoopt);
tc.addOptionHandler(gaopt);
tc.connect(host, port);
in = tc.getInputStream();
out = new PrintStream(tc.getOutputStream());
return true;
} catch (Exception e) {
e.printStackTrace();
try {
tc.disconnect();
in.close();
out.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return false;
}
} private void closeconnect() {
try {
tc.disconnect();
in.close();
//out.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
} private String printresponse() {
try {
byte[] buff = new byte[1024];
int ret_read = 0;
do {
ret_read = in.read(buff);
String a = new String(buff, 0, ret_read);
System.out.print("=" + a);
if (a.endsWith(":") | a.endsWith(">") | a.endsWith("]")) {
System.out.print(a);
return null;
} } while (ret_read >= 0);
} catch (Exception e) {
System.err.println("Exception while reading socket:"
+ e.getMessage());
} try {
tc.disconnect();
} catch (Exception e) {
System.err.println("Exception while closing telnet:"
+ e.getMessage());
}
return null;
} private String printresponse2() {
try {
byte[] buff = new byte[1024];
int ret_read = 0;
ret_read = in.read(buff);
while (ret_read >= 0) {
String a = new String(buff, 0, ret_read);
a = a.trim();
System.out.print("=" + a);
if (a.endsWith(":") | a.endsWith(">") | a.endsWith("]")) {
System.out.print(a);
return null;
}
}
} catch (Exception e) {
System.err.println("Exception while reading socket:"
+ e.getMessage());
} try {
tc.disconnect();
} catch (Exception e) {
System.err.println("Exception while closing telnet:"
+ e.getMessage());
}
return null;
}}返回的是
[5;1H[K[6;1H[K[7;1H[K[8;1H[K[9;1H[K[10;1H[K[11;1H[K[12;1H[K[13;1H[K[14;1H[K[15;1H[K[16;1H[K[17;1H[K[18;1H[K[19;1H[K[20;1H[K
那位高手有解决的方法
我是新手没有多少分
这个东西好像叫做终端转意字符。具体英文忘记了。字符串的内容是怎么在一个屏幕上显示字符串。包括位置颜色什么的。有协议专门说这个,rfc文档有说这个东西。自己解析比较累,反正我最后放弃了。很多java开源的telnet工具都实现,当初看着比较复杂,也没有什么动力,不了了之了。给你的建议也只能使找个开源工具去查源码。
wiki:
http://en.wikipedia.org/wiki/Terminal_emulator上面描述比较清楚,不过涉及很多很多东西。
我放弃了!自己玩这个东西,有点狠。
private TelnetClient telnet = new TelnetClient();
远程登录AIX、linux、sun 服务器执行脚本返回信息都正常,但远程登录windows执行脚本返回信息是乱码。 后来在new TelnetClient()的时候设置了参数,如下所:
private TelnetClient telnet = new TelnetClient("VT220");
登录Windows 2003 和XP执行脚本返回信息就正常了。 但是Windows 2000还是不行!
是不是TelnetClient不支持2000呀,麻烦大哥们分析分析!