寻找Java Telnet模拟器 [英] Looking for Java Telnet emulator

查看:178
本文介绍了寻找Java Telnet模拟器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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

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

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

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

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,接下来的两个选项是:

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

JExpect - 这是不是那么难用,做我需要的但支持得多好吗?它是否适用于JDK 6,我认为是这样。

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.

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

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)

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

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.

谢谢。

推荐答案

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

Found what I was looking for here: 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();
        }
    }
}

这篇关于寻找Java Telnet模拟器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆