How to send and receive commands via telnet in a java application?

1

I am trying to make a program for the sample of meteorological parameters that come from another remote computer. Until the moment with the code that I have implemented I only have the connection with the remote computer but I can not get the way to send commands and receive it, who can help me would be appreciated. It should be noted that I am new in this programming but it is extremely necessary to do this because my degree work depends on this connection to make the end of the application that I must design to complement it. The code used is as follows.

    import java.io.InputStream;
    import java.io.PrintStream;
    import 
   org.apache.commons.net.telnet.TelnetClient;
  //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(
                "127.0.0.1", "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();
    }
}

}

    
asked by Jimi Noriega 08.04.2018 в 17:41
source

0 answers