因此,我有使用 org.apache.commons.net.telnet.TelnetClient
类的此类。它尝试发送命令并读取响应。
public class AutomatedTelnetClient
{
private TelnetClient telnet = new TelnetClient();
private InputStream in;
private PrintStream out;
private String prompt = "$";
public AutomatedTelnetClient(String server, String user, String password)
{
try
{
EchoOptionHandler echoopt = new EchoOptionHandler(false, false, false, false);
telnet.addOptionHandler(echoopt);
// Connect to the specified server
telnet.connect(server, 23);
// Get input and output stream references
in = telnet.getInputStream();
out = new PrintStream(telnet.getOutputStream());
// Log the user on
readUntil("login: ");
write(user);
readUntil("Password: ");
write(password);
// Advance to a prompt
readUntil(prompt + " ");
}
catch (Exception e)
{
e.printStackTrace();
}
}
public void su(String password)
{
try
{
write("su");
readUntil("Password: ");
write(password);
prompt = "#";
readUntil(prompt + " ");
}
catch (Exception e)
{
e.printStackTrace();
}
}
public String readUntil(String pattern)
{
try
{
char lastChar = pattern.charAt(pattern.length() - 1);
StringBuffer sb = new StringBuffer();
boolean found = false;
char ch = (char) in.read();
while (true)
{
System.out.print(ch);
sb.append(ch);
if (ch == lastChar)
{
if (sb.toString().endsWith(pattern))
{
return sb.toString();
}
}
ch = (char) in.read();
}
}
catch (Exception e)
{
e.printStackTrace();
}
return null;
}
public void write(String value)
{
try
{
out.println(value);
out.flush();
System.out.println(value);
}
catch (Exception e)
{
e.printStackTrace();
}
}
public String sendCommand(String command)
{
try
{
write(command);
return readUntil(prompt + " ");
}
catch (Exception e)
{
e.printStackTrace();
}
return null;
}
public void disconnect()
{
try
{
telnet.disconnect();
}
catch (Exception e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
try
{
AutomatedTelnetClient telnet = new AutomatedTelnetClient(
"...", "...", "...");
System.out.println("Got Connection...");
System.out.println("run command");
telnet.sendCommand("ls ");
telnet.sendCommand("cd netConf");
telnet.sendCommand("ls ");
telnet.sendCommand("cd lanSetup");
telnet.sendCommand("ls ");
telnet.sendCommand("cd dhcpd");
telnet.sendCommand("show interface 2");
telnet.disconnect();
System.out.println("DONE");
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
麻烦的是,当我发送命令时,响应以各种回显开头。例如,当我发送ls
命令时,这是我读取的响应
[mls
....
我尝试在how to disable echo上搜索,但似乎没人能找到答案。所以,我决定问这个社区。有谁知道如何禁用此回声?
编辑
查看 EchoOptionHandler
的源代码使我感到困惑。为什么子协商方法仅返回null
?
参考方案
有趣的问题。总结一下我的努力:我没有使其正常工作
但是这是我的一些发现:
您不能将IAC DON'T ECHO
直接写到数据通道,这是通过示例中的命令和选项完成的
int[] msg = {TelnetCommand.DONT,TelnetOption.ECHO};
telnet.sendSubnegotiation(msg);
您可以注册telnet.registerNotifHandler(new MyNotificationHandler());
以在连接建立期间调试命令
public class MyNotificationHandler implements TelnetNotificationHandler
{
@Override
public void receivedNegotiation(int negotiation_code, int option_code)
{
System.out.println("--->"+get(negotiation_code)+" "
+TelnetOption.getOption(option_code));
}
private String get(int i)
{
if(i==TelnetNotificationHandler.RECEIVED_DO){return "RECEIVED_DO";}
else if(i==TelnetNotificationHandler.RECEIVED_DONT){return "RECEIVED_DONT";}
else if(i==TelnetNotificationHandler.RECEIVED_WILL){return "RECEIVED_WILL";}
else if(i==TelnetNotificationHandler.RECEIVED_WONT){return "RECEIVED_WONT";}
else if(i==TelnetNotificationHandler.RECEIVED_COMMAND)
{return "RECEIVED_COMMAND";}
return "UNKNOWN";
}
}
java.net.UnknownHostException无法连接到ftp - java我的ftp端口为:ftp://173.201.0.1/我正在尝试通过以下方式将其连接:String Ftp_Path = "ftp://173.201.0.1/"; public List<String> GetFileList() { String ftpServerIP = Ftp_Path; String ftpUser…
java.net.URI.create异常 - javajava.net.URI.create("http://adserver.adtech.de/adlink|3.0") 抛出java.net.URISyntaxException: Illegal character in path at index 32: http://adserver.adtech.de/adlink|3.0 虽然n…
从JAVA调用方法C#.NET - java我有一个C#.NET项目中创建的dll,我想从Java程序中调用方法。我想知道是否存在实现此目标的机会,然后遇到了JNA和JNI。我应该使用哪一个?有什么建议么?我只需要在使用C#.NET编写的类中调用方法并处理Java程序的结果即可。 参考方案 这取决于您的应用程序,但是您可以将C#DLL放在服务中,例如WCF并以这种方式将功能公开给Java代码。使用ws…
在Apache Commons IO中使用依赖项注入 - java我有一个使用Apache Commons IO的FileUtils类复制目录的类'a'。因为我必须为类'a'编写单元测试,所以我需要能够注入所有依赖项以在测试中模拟它们。但是如何注入FileUtils类呢?没有接口,所有方法都是抽象的。 参考方案 实用程序类的所有方法都是静态的。您不能嘲笑它们(可以,通过字节码操作,但是很丑陋)。处理该问题的正确方法是提供对…
Java-搜索字符串数组中的字符串 - java在Java中,我们是否有任何方法可以发现特定字符串是字符串数组的一部分。我可以避免出现一个循环。例如String [] array = {"AA","BB","CC" }; string x = "BB" 我想要一个if (some condition to tell wheth…