下面的代码能Telnet到远程主机,并执行脚本。但是在执行write(command)时,如果不用for(int i=0;i<800000000;i++);那么程序等不到接收到的数据就退出了。怎么添加个线程,可以在发送信息(out.println)的时候,可以同步接受信息(printresponse)呢?
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.5.55",23,"root","1234");
} public TmhTelnet(String host, int port, String username, String password) {
byte[] a = new byte[1024];
if (intconnect(host, port)) {
write(username);
write(password);
write("test.bat");
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(); // for(int i=0;i<800000000;i++);
// 应该使用线程同步使程序在接受到对方相应后再执行下一条命令这里使用for循环来模拟对方的相应时间
printresponse();
} catch (Exception e) {
e.printStackTrace();
}
} private boolean intconnect(String host, int port) {
try {
tc = new TelnetClient();
TerminalTypeOptionHandler ttopt = new TerminalTypeOptionHandler(
"VT220", 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);
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;
}}
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.5.55",23,"root","1234");
} public TmhTelnet(String host, int port, String username, String password) {
byte[] a = new byte[1024];
if (intconnect(host, port)) {
write(username);
write(password);
write("test.bat");
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(); // for(int i=0;i<800000000;i++);
// 应该使用线程同步使程序在接受到对方相应后再执行下一条命令这里使用for循环来模拟对方的相应时间
printresponse();
} catch (Exception e) {
e.printStackTrace();
}
} private boolean intconnect(String host, int port) {
try {
tc = new TelnetClient();
TerminalTypeOptionHandler ttopt = new TerminalTypeOptionHandler(
"VT220", 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);
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;
}}
解决方案 »
- 一个表单的表单内容和他的确定按钮在不同的jsp页面 怎么才能成功的提交表单呢?急急急。。。。。。。。。。。
- org.springframework.beans.factory.BeanDefinitionStoreException
- struts2标签应用
- tomcat5.5和apache 频繁报错 80分
- 求助,查询数据库时,出个怪问题!
- 一个奇怪的struts问题
- JTabbedPane的问题
- [如果可以给800分]在Jbuilder和weblogic中如何实现下面的多对多的ejb(一个班级对多个教师,一个教师教多个班级,bjrkjsxx为bjxx和jsxx之
- 关于JNDI&LDAP的问题 !!! 急
- 找不到包javax.management.*;com.ibm.websphere.management.*;不知道为什么,该如何去添加
- 变态的需求,请高手赐教
- 怎么Servlet里面获取不到值页面超链接里面带的参数的值啊,求解释
问题还没解决~~~~~
问题还没解决~~~~~
问题还没解决~~~~~