Looking for Java Telnet emulator [closed]

2019-01-16 12:54发布

问题:

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.

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

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

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.

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

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 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)

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.

Thanks.

回答1:

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


回答2:

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



回答3:

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



回答4:

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

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



标签: java telnet