Search code examples
javawebtelnetping

Web app that runs commands in remote machine


I want to develop a web app that runs commands like "ping" or "telnet" to a remote host. The app should be accessible from any machine.

For example, I should be able to open a telnet session to my home PC from anywhere. Please I need ideas not codes.

public class Ping extends HttpServlet{  
    public void doGet( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException{
        Process p = Runtime.getRuntime().exec("ping google.com");

        BufferedReader inputStream = new BufferedReader(
            new InputStreamReader(p.getInputStream()));

        String res=""; 
        String s = ""; while ((s = inputStream.readLine()) != null) {res=res+s+"\n";}

        request.setAttribute( "test", res );

        this.getServletContext().getRequestDispatcher( "/WEB-INF/test.jsp" ).forward( request, response );
    }
}

Solution

  • Split your webapp into two parts, one in charge of interacting with the browser, and another to do the actual work.

    1. The front-end generates HTML (probably using JSP, since you are using Java) with

      • a button labelled 'ping', and when the button is pressed, sends a form and displays the ping results
      • a text input labelled 'telnet' with a button labelled 'send', and when the button is pressed, sends a form and displays the output of the command
    2. The back-end is Java code to actually perform 'ping' or 'telnet' on request, and capture the results so that they can be reported back via the front-end. Although you can implement both protocols via Java, it is probably much easier to launch existing ping and ssh clients (ssh = secure telnet; telnet should not be used), redirect their output to a file, and return the contents of the file.

    Part 2 does not need to be aware at all that it is being used from part 1 - it is plain Java code that implements something like

    public static String ping(String ip, int timeoutMs) {
       // returns ping output as a String, or error if timeout reached
    }
    
    public static String telnet(String ip, int port, String command, int timeoutMs) {
       // returns output of ssh -c command to chosen ip:port
       // must have public-key login to chosen machine for this to work 
       //   without password prompts
       // returns error description if timeout reached 
    }