寻找Java Telnet模拟器

我正在编写一个后端程序,telnet到服务器,运行一些命令并保存这些命令的所有输出。 像Expect一样的东西。

我想使用一个受到良好支持并使用JDK 6运行的开源解决方案。

到目前为止,我找到了3个选项,并希望能够帮助决定使用哪个(或更好的建议)。

commons-net – 这得到了很好的支持,但我无法通过简单的“登录并执行’ls’命令工作。 我更喜欢使用这个库,如果任何人都可以提供一个简单的例子(而不是带有来自用户的输入的示例)我想走那条路。

如果我无法使用commons-net,则接下来的两个选项是:

JExpect – 这不是很难使用,我需要的是什么,但支持得有多好? 它是否适用于JDK 6,我想是的。

Java Telnet应用程序(jta26) – 这很容易使用,但我不确定它是多么通用。 我没有在TelnetWrapper中看到任何设置超时值的地方。 自从上次更新网站是在2005年以来,我也不确定是否正在维护此代码。( http://www.javassh.org )

我知道这有些是以意见为导向的,希望SO是一个帮助我做出决定的好地方所以我不会从一条路开始,后来发现它不是我想要的。

谢谢。

找到我在这里寻找的东西: http : //twit88.com/blog/2007/12/22/java-writing-an-automated-telnet-client/

您需要修改提示变量。

代码副本:

import org.apache.commons.net.telnet.TelnetClient; import java.io.InputStream; import java.io.PrintStream; 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 { // 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( "myserver", "userId", "Password"); System.out.println("Got Connection..."); telnet.sendCommand("ps -ef "); System.out.println("run command"); telnet.sendCommand("ls "); System.out.println("run command 2"); telnet.disconnect(); System.out.println("DONE"); } catch (Exception e) { e.printStackTrace(); } } } 

你看过Sadun utils图书馆了吗? 我用它一次打开到服务器的telnet会话并发送一些命令,读取响应,然后关闭连接,它工作正常,它是LGPL

试试http://www.java2s.com/Code/Java/Network-Protocol/ExampleofuseofTelnetClient.htm 。

AutomatedTelnetClient运行良好。 经过长时间的搜索,很高兴看到一个简单的工作程序:)。

我刚刚修改了提示符$并删除了最后的空格,所有命令都正常工作。