寻找 Java Telnet 模拟器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1195809/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 15:35:06  来源:igfitidea点击:

Looking for Java Telnet emulator

javatelnet

提问by Ben

I am writing a back end program that telnets into a server, runs some commands and saves all the output from those commands. Something just like Expect.

我正在编写一个后端程序,该程序可以远程登录到服务器,运行一些命令并保存这些命令的所有输出。就像Expect一样。

I would like to use an open source solution that is well supported and runs with JDK 6.

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

I have found 3 options so far and would like some help deciding which one (or a better suggestion) to use.

到目前为止,我已经找到了 3 个选项,并且需要一些帮助来决定使用哪一个(或更好的建议)。

commons-net – This is very well supported but I having trouble getting a simple “Log in and do ‘ls'” command working. I would prefer to use this library, if anyone can provide a simple example (and not the example that comes with it that takes input from the user) I would like to go that route.

commons-net – 这得到了很好的支持,但我无法获得一个简单的“登录并执行 'ls'”命令。我更喜欢使用这个库,如果有人可以提供一个简单的例子(而不是它附带的从用户那里获取输入的例子),我想走那条路。

If I'm unable to use commons-net the next two options are:

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

JExpect – This is not that hard to use, does what I need but how well supported is it? Will it work with JDK 6, I think so.

JExpect – 这并不难使用,我需要什么,但它的支持程度如何?它是否适用于 JDK 6,我认为是这样。

Java Telnet Application (jta26) – This was easy to use but I'm not sure how versatile it is. I didn't see any place to set a timeout value in the TelnetWrapper. I also was not sure if this code is being maintained since the last update to the site was in 2005. (http://www.javassh.org)

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

I know this is somewhat opinion oriented and hope SO is a good place to help me make a decision so I don't start down one road and find out later it's not what I'm looking for.

我知道这有点以意见为导向,希望 SO 是一个帮助我做出决定的好地方,这样我就不会走上一条路,后来发现这不是我想要的。

Thanks.

谢谢。

采纳答案by Ben

Found what I was looking for here: http://twit88.com/blog/2007/12/22/java-writing-an-automated-telnet-client/

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

You will need to modify the prompt variable.

您将需要修改提示变量。

Copy of code:

代码副本:

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();
        }
    }
}

回答by Naveen

AutomatedTelnetClientworks well. After a long search, nice to see one simple working program:).

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

I just modified prompt to $and removed empty spaces at the end and all commands working.

我只是修改了 prompt$并删除了最后的空格并且所有命令都在工作。

回答by Chochos

Have you looked at the Sadun utils library? I used it once to open a telnet session to a server and send some commands, read a response, and close the connection, it worked fine and it's LGPL

你看过Sadun utils 库吗?我曾经用它打开一个到服务器的 telnet 会话并发送一些命令、读取响应并关闭连接,它工作正常,它是 LGPL