我正在使用 apache.commons.net.telnet 为 cisco 路由器编写 telnet 客户端。但我有问题。这是代码示例:
static TelnetClient telnetClient = new TelnetClient();
public static void main(String[] args) throws IOException {
setOptionHandlers();
telnetClient.connect("192.168.127.100");
read();
telnetClient.disconnect();
}
private static void setOptionHandlers() throws IOException {
ArrayList<TelnetOptionHandler> optionHandlers =
new ArrayList<TelnetOptionHandler>();
optionHandlers.add(new TerminalTypeOptionHandler("VT100", false, false, true, false));
optionHandlers.add(new EchoOptionHandler(true, false, true, false));
optionHandlers.add(new SuppressGAOptionHandler(true, true, true, true));
for (TelnetOptionHandler handler : optionHandlers) {
try {
telnetClient.addOptionHandler(handler);
}
catch (InvalidTelnetOptionException e) {
System.err.println("Error registering option handler "
+ handler.getClass().getSimpleName());
}
}
}
public static void write(byte[] data) throws IOException {
telnetClient.getOutputStream().write(data);
telnetClient.getOutputStream().flush();
}
public static void read() throws IOException {
System.out.println("Read");
byte[] buff = new byte[1024];
int read;
if((read = telnetClient.getInputStream().read(buff)) > 0) {
System.out.println(new String(buff, 0, read));
}
System.out.println("read="+read);
}
在某些情况下,它可以正常工作并提示输入密码。但在其他情况下它是否工作不正确 - 通过从 telnet 输入流中读取而挂起。运行条件相同。为什么我会遇到这种情况?如果有人有编写 cisco telnet 客户端的技巧,我会很高兴听到他们!