public void doJob() {
// restartTerminal();
counter();
}
private void counter() {
//演示在一台机器上远程登录另一台计算机
try {
readUntil("bash-2.05b$ ");
write("ssh loc@192.168.0.1\n");
readUntil("loc@192.168.0.1's password: ");
write("nodee\n");
readUntil("[nodee@dz-05 ~]$ ");
write("ll\n");
readToEnd();
} catch (Exception e) {
e.printStackTrace();
} finally {
}
}
public static void main(String[] args) {
String hostname = "192.168.0.2";
int hostport = 23;
String user = "username";
String password = "pwd";
TelnetHelper_bak helper = null;
try {
helper = new TelnetHelper_bak(hostname, hostport, user, password);
helper.doJob();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (helper != null)
helper.close();
}
}
/**
* 读取主线程,负责管理子线程。防止读取时不动了,这时就抛弃读取子线程
*
*/
class ReadThread extends Thread {
public void run() {
synchronized (lock) {//只能一个读取
SubReadThread sub = new SubReadThread();
sub.start();
int last = sub.count;
while (true) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
}
if (last == sub.count) {
sub.stop();
break;
} else {
last = sub.count;
}
}
String s = sub.sb.toString();
try {
System.out.println(new String(s.getBytes(ORIG_CODEC),
TRANSLATE_CODEC));
} catch (UnsupportedEncodingException e) {
System.out.println(s);
}
sub = null;
}
// System.out.println("===========ReadThread end=============");
}
}