程序功能目标:通过一个已经连接telnet 发送另外一条telnet 命令跳转到跳转到另外一个主机。(第一个支持telnet命令)
代码如下:
[code="java"]
package tmh.tvs.util.net.telnet;
import org.apache.commons.net.telnet.*;
import java.io.*;
import java.net.SocketException;
import java.util.StringTokenizer;
public class TmhTelnet implements Runnable, TelnetNotificationHandler {
private static TelnetClient tc = null;
private InputStream in;
private PrintStream out;
public static void main(String args[]){
Thread t = new Thread(new TmhTelnet("125.46.86.123",23,"*****","******"));
t.start();
}
public TmhTelnet(String host, int port,String username,String password) {
if(intconnect(host,port)){
write(username);
write(password);
writescript("command.txt");
}
}
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();
}
}
private void write(String command) {
try{
System.out.println("command:>>>>>>>>>"+command);
out.println(command);
out.flush();
}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();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return false;
}
}
public void receivedNegotiation(int negotiation_code, int option_code) {
String command = null;
if (negotiation_code == TelnetNotificationHandler.RECEIVED_DO) {
command = "DO";
} else if (negotiation_code == TelnetNotificationHandler.RECEIVED_DONT) {
command = "DONT";
} else if (negotiation_code == TelnetNotificationHandler.RECEIVED_WILL) {
command = "WILL";
} else if (negotiation_code == TelnetNotificationHandler.RECEIVED_WONT) {
command = "WONT";
}
System.out.println("Received " + command + " for option code "
+ option_code);
}
/***************************************************************************
* Reader thread. Reads lines from the TelnetClient and echoes them on the
* screen.
**************************************************************************/
public void run() {
InputStream instr = tc.getInputStream();
try {
byte[] buff = new byte[1024];
int ret_read = 0;
do {
ret_read = instr.read(buff);
if (ret_read > 0) {
System.out.print(new String(buff, 0, ret_read));
}
} 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());
}
}
}
[/code]
输出结果:
command:>>>>>>>>>*****
command:>>>>>>>>>*******
command:>>>>>>>>>telnet 10.1.2.2
command:>>>>>>>>>******
command:>>>>>>>>>dis mac
command:>>>>>>>>>null
- Copyright (c) 2004-2007 Hangzhou H3C Tech. Co., Ltd. All rights reserved. *
- Without the owner's prior written consent, *
- no decompiling or reverse-engineering shall be allowed. * ********************************************************************************
Login authentication
Username:admin
Password:
telnet 10.1.2.2
Trying 10.1.2.2 ...
Press CTRL+K to abort
Connected to 10.1.2.2 ...password:在这里应该是向下走的可是不行(很不解)
问题:在使用键盘输入的时候可以跳转,但是使用批处理的时候只到了要求密码的地方。请各位给点建议。我用的apache的commons net 包