寻找Java Telnet仿真器

17

我正在编写一个后端程序,它通过telnet连接到服务器,运行一些命令并保存所有的输出结果。类似于Expect。

我希望使用一个开源解决方案,该方案得到很好的支持,并且可以在JDK 6上运行。

目前我找到了三个选项,并想要一些帮助来决定使用哪一个(或者是否有更好的建议)。

commons-net – 这个库得到了很好的支持,但是我遇到了一些问题,在尝试简单的“登录并执行‘ls’”命令时遇到了麻烦。如果有人能够提供一个简单的示例(而不是来自用户的示例),我将更喜欢使用这个库。

如果无法使用commons-net,则下面两个选项可供选择:

JExpect – 这个库不难使用,可以实现我需要的功能,但是它的支持程度如何?它能在JDK 6中工作吗?我认为可以。

Java Telnet Application (jta26) – 这个库使用很容易,但我不确定它的灵活性如何。我没有看到在TelnetWrapper中设置超时值的地方。我也不确定这个代码是否正在维护,因为站点上的最后更新时间是2005年。(http://www.javassh.org

我知道这有些基于个人意见,但我希望Stack Overflow是一个可以帮助我做出决定的好地方,这样我就不会走错路,后来发现它不是我要找的东西。

谢谢。

4个回答

17

我在这里找到了我需要的东西: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();
        }
    }
}

可能需要清除密码。 - Dave Jarvis

0

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

我只是将提示符修改为 $ 并删除了末尾的空格,所有命令都可以正常工作。


0
你看过Sadun utils库吗?我曾经用它打开一个telnet会话到服务器并发送一些命令,读取响应并关闭连接,它运行良好且是LGPL许可证。

看起来这个方案可以行得通,但是我和其他解决方案一样遇到了同样的问题,它的支持不够好。我可能会使用JExpect或JTA代替。 - Ben

0

那正是我不需要的例子。我看过它,但我不明白它是如何工作的。我知道它从用户那里获取输入,而这不是我要找的。我应该在哪里输入“ls”命令并查看输出? - Ben

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接