Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: opening Telnet Command Session

  1. #1
    Junior Member
    Join Date
    Jun 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question opening Telnet Command Session

    Hi,

    I want to automate the telnet login in XP where in need the telnet session open in a different window. For this I am using "Process/ProcessBuilder" and then tried to write into the OutputStream of the telnet process. But I am not getting the desired result. Need your help.

    The code looks like :
               // cmd is " CMD.EXE /C start telnet <ip>"
                ProcessBuilder processBuilder = new ProcessBuilder(cmd);
                processBuilder.directory(new File(System.getenv("temp")));
                processBuilder.redirectErrorStream(true);
     
                Process proc = processBuilder.start();
     
               System.out.println("Trying to pass the telnet  user ID for the login: promt..");
               OutputStream out = proc.getOutputStream();
                out.write("user\n".getBytes());
                out.close();
     
               System.out.println("Trying to pass the telnet  password for the password: promt..");
               OutputStream out = proc.getOutputStream();
                out.write("pass\n".getBytes());
                out.close();
                //Some other command sequence....

    The telnet session comes up then other data passed to the child process doesn't seem to have any apparent effect. It is required for me to remain the telenet window open.....

    Any suggestion?
    Last edited by JavaPF; June 19th, 2009 at 08:06 AM. Reason: Please use [code] [/code] tags


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: opening Telnet Command Session

    Hello voyager. Welcome to the Java Programming Forums

    Can you please post all of your code for me to compile? Thanks.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  3. #3
    Junior Member
    Join Date
    Jun 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: opening Telnet Command Session

    Thank you JavaPF,

    For a quick response

    For testing the below code will simulate the things:

    import java.util.StringTokenizer;
    import java.io.*;
     
    class TelnetTest {
     
        public static void main(String args[]) {
            String ip = "<give an IP address to connect>";//or pass it as an argument
            String strCMD = "CMD.EXE /C start telnet " + ip;
     
            StringTokenizer st = new StringTokenizer(strCMD);
            String[] cmd = new String[st.countTokens()];
            int total = st.countTokens();
            for (int i = 0; i < total; i++) {
                cmd[i] = st.nextToken(" ");
            }
     
            try {
     
                ProcessBuilder processBuilder = new ProcessBuilder(cmd);
                processBuilder.directory(new File(System.getenv("temp")));
                processBuilder.redirectErrorStream(true);
     
                Process proc = processBuilder.start();
     
                /*
                 * Now the child process is on and waiting for user ID
                 */
                System.out.println("Trying to pass the telnet  user ID for the login: promt..");
                OutputStream out = proc.getOutputStream();//This should give me the access to stdout of the child
                out.write("user\n".getBytes());//provide a valid user name
                out.flush();
     
                System.out.println("Trying to pass the telnet  password for the password: promt..");
     
                out.write("pass\n".getBytes());
                out.close();
     
                int status = proc.waitFor();
     
     
            } catch (Throwable t) {
                t.printStackTrace();
            }
        }
    }

    Here I am not trying to use InputStream because I expect it to be visible in the telnet window.

    Thanks again....

  4. #4
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: opening Telnet Command Session

    Hello voyager,

    I haven't really played with the ProcessBuilder before so I'm not sure whats going on. Where did you get this code example from?
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

Similar Threads

  1. How can i put session in Javascript?
    By rajani in forum JavaServer Pages: JSP & JSTL
    Replies: 2
    Last Post: July 8th, 2009, 12:46 PM
  2. How to Send command line arguments in Eclipse?
    By JavaPF in forum Java JDK & IDE Tutorials
    Replies: 0
    Last Post: April 23rd, 2009, 11:37 AM

Tags for this Thread