急求java实现telnet登陆全过程的源码 参考 http://www.javaeye.com/problems/957 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 很简单,你知道知道telnet的协议就行了,很简单。然后UDP。 楼主的代码不是同这个吗?http://topic.csdn.net/t/20050606/15/4062913.html 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.37", 23, "administrator", "222"); } 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谁该看下 请问楼上org.apache.commons.net.telnet的这个包从哪里来的?我查看了下tomcat5的lib里面并没有发现,可以的话最好能提供下载地址,谢谢! 两个文本框 焦点的问题 请问为什么下面的程序画不出来矩形呢 有关哈希表的问题 怎么用JAVA编写一个文本编辑工具,类似记事本的!急急急!!!! 菜鸟求助毕业设计中的问题 --->请注重基本功的程序员进来看看 synchronized(new Integer(0))这代表什么意思啊? 请问java程序中如何调用HLP或者chm文件? 有什么java程序可以根据一个xml生成java代码 请问那里有介绍java入门的资料,我只想知道应该学java那一种呢? JAVA中AES算法的密钥强度问题,使用过AES的来 急啊!
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.37", 23, "administrator", "222");
} 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
谁该看下