telnet from Java

Discussion in 'Java' started by Wilson Chew, Jul 29, 2004.

  1. Wilson Chew

    Wilson Chew Guest

    hi,

    Say I am using a Windows machine, can I use Java to programmatically telnet
    to a unix machine?
    Here's what I did:

    String[] cmd = new String[3];

    cmd[0] = "cmd.exe";
    cmd[1] = "/c";
    cmd[2] = "telnet unixMachine";

    process = Runtime.getRuntime().exec(cmd);

    PrintWriter pr = new PrintWriter(process.getOutputStream());
    pr.print("username"); //print username to process
    pr.print("password"); //print password to process

    This doesn't seems to work. Anybody has experience with this?

    Thanks in advance !!

    Wilson Chew
    Wilson Chew, Jul 29, 2004
    #1
    1. Advertising

  2. Wilson Chew

    John Fereira Guest

    "Wilson Chew" <> wrote in
    news:ceblrs$mrn$:

    > hi,
    >
    > Say I am using a Windows machine, can I use Java to programmatically
    > telnet to a unix machine?
    > Here's what I did:
    >
    > String[] cmd = new String[3];
    >
    > cmd[0] = "cmd.exe";
    > cmd[1] = "/c";
    > cmd[2] = "telnet unixMachine";
    >
    > process = Runtime.getRuntime().exec(cmd);
    >
    > PrintWriter pr = new
    > PrintWriter(process.getOutputStream());
    > pr.print("username"); //print username to process
    > pr.print("password"); //print password to process
    >
    > This doesn't seems to work. Anybody has experience with this?


    Take a look at commons-net package. It's based on the old NetComponents
    package and supports Telnet, Ftp, SMTP, NNTP, and several other protocols.
    I've used it in the past to automate FTP transfers and it works well.
    John Fereira, Jul 29, 2004
    #2
    1. Advertising

  3. Wilson Chew

    marcus Guest

    This is a major pet peeve of mine . . .
    What is the word "telnet"? What does it represent? Is it just the name
    of a windows program? Hmm, could it be a protocol?

    what does the word "protocol" mean? Is it a standard method of doing
    something, like diplomatic protocol? An agreed standard of communication?

    Hmm, maybe you should do something simple, like google "telnet protocol"
    before demonstrating to any prospective employer who googles "Wilson
    Chew" how blazingly inept you are at problem solving.

    http://www.scit.wlv.ac.uk/~jphb/comms/telnet.html

    Then, when you see how silly rewriting an entire communications protocol
    would be, try googling c.l.j.p for telnet package.

    Better yet, pick up a paintbrush.

    -- clh

    Wilson Chew wrote:
    > hi,
    >
    > Say I am using a Windows machine, can I use Java to programmatically telnet
    > to a unix machine?
    > Here's what I did:
    >
    > String[] cmd = new String[3];
    >
    > cmd[0] = "cmd.exe";
    > cmd[1] = "/c";
    > cmd[2] = "telnet unixMachine";
    >
    > process = Runtime.getRuntime().exec(cmd);
    >
    > PrintWriter pr = new PrintWriter(process.getOutputStream());
    > pr.print("username"); //print username to process
    > pr.print("password"); //print password to process
    >
    > This doesn't seems to work. Anybody has experience with this?
    >
    > Thanks in advance !!
    >
    > Wilson Chew
    >
    >
    marcus, Jul 30, 2004
    #3
  4. Wilson Chew

    KC Wong Guest

    @marcus:

    The OP didn't ask to rewrite a telnet client in Java... look at the code the
    OP provided. Runtime is created and PrintWriter is used to pump input into
    it.

    @Wilson:

    I haven't tried that, but telnet isn't secure - you don't want to use it.
    Use SSH instead... it also talks telnet, and a free client is available for
    Win32, Unix and many other platforms.

    It's called PuTTY: http://www.chiark.greenend.org.uk/~sgtatham/putty/

    It supports commandline mode, where you can tell it to use a script file.
    KC Wong, Jul 30, 2004
    #4
  5. Wilson Chew

    zoopy Guest

    On 29-7-2004 22:15, Wilson Chew wrote:
    > hi,
    >
    > Say I am using a Windows machine, can I use Java to programmatically telnet
    > to a unix machine?
    > Here's what I did:
    >
    > String[] cmd = new String[3];
    >
    > cmd[0] = "cmd.exe";
    > cmd[1] = "/c";
    > cmd[2] = "telnet unixMachine";
    >
    > process = Runtime.getRuntime().exec(cmd);
    >
    > PrintWriter pr = new PrintWriter(process.getOutputStream());
    > pr.print("username"); //print username to process
    > pr.print("password"); //print password to process
    >
    > This doesn't seems to work. Anybody has experience with this?
    >
    > Thanks in advance !!
    >
    > Wilson Chew
    >
    >

    Same question came up a few days ago. See
    <http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&c2coff=1&threadm=f2f52eb3.0407260907.4616b67d%40posting.google.com&rnum=1&prev=/groups%3Fhl%3Den%26lr%3D%26ie%3DUTF-8%26c2coff%3D1%26selm%3Df2f52eb3.0407260907.4616b67d%2540posting.google.com>
    and
    <http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&c2coff=1&threadm=f2f52eb3.0407270615.5d365217%40posting.google.com&rnum=1&prev=/groups%3Fhl%3Den%26lr%3D%26ie%3DUTF-8%26c2coff%3D1%26selm%3Df2f52eb3.0407270615.5d365217%2540posting.google.com>

    HTH,
    Z.
    zoopy, Jul 30, 2004
    #5
  6. Wilson Chew

    marcus Guest

    and the difference between writing a telnet client and writing a program
    that emulates a telnet client for the purposes of communicating via
    telnet protocol is . . . ?

    KC Wong wrote:
    > @marcus:
    >
    > The OP didn't ask to rewrite a telnet client in Java... look at the code the
    > OP provided. Runtime is created and PrintWriter is used to pump input into
    > it.
    >
    > @Wilson:
    >
    > I haven't tried that, but telnet isn't secure - you don't want to use it.
    > Use SSH instead... it also talks telnet, and a free client is available for
    > Win32, Unix and many other platforms.
    >
    > It's called PuTTY: http://www.chiark.greenend.org.uk/~sgtatham/putty/
    >
    > It supports commandline mode, where you can tell it to use a script file.
    >
    >
    marcus, Jul 31, 2004
    #6
  7. Wilson Chew wrote:
    > hi,
    >
    > Say I am using a Windows machine, can I use Java to programmatically telnet
    > to a unix machine?
    > Here's what I did:
    >
    > String[] cmd = new String[3];
    >
    > cmd[0] = "cmd.exe";
    > cmd[1] = "/c";
    > cmd[2] = "telnet unixMachine";
    >
    > process = Runtime.getRuntime().exec(cmd);
    >
    > PrintWriter pr = new PrintWriter(process.getOutputStream());
    > pr.print("username"); //print username to process
    > pr.print("password"); //print password to process
    >
    > This doesn't seems to work. Anybody has experience with this?
    >
    > Thanks in advance !!
    >
    > Wilson Chew
    >
    >


    It's a start, but you have a way to go. You will also need to use the stdout and
    stderr streams from the client and read the messages returned from the server.
    Failing to do so may result in a hang because the Process's output buffers
    are full.

    The immediate reaon this doesn't log in is that you forgot to send the enter
    key after the name and password. Try:
    pr.print(username + "\r");
    or:
    pr.println(username);

    Steve
    Steve Horsley, Jul 31, 2004
    #7
  8. Wilson Chew

    marcus Guest

    incorrect -- the reason it doesn't work is because he failed to complete
    the protocol handshake.

    Steve Horsley wrote:
    > Wilson Chew wrote:
    >
    >> hi,
    >>
    >> Say I am using a Windows machine, can I use Java to programmatically
    >> telnet
    >> to a unix machine?
    >> Here's what I did:
    >>
    >> String[] cmd = new String[3];
    >>
    >> cmd[0] = "cmd.exe";
    >> cmd[1] = "/c";
    >> cmd[2] = "telnet unixMachine";
    >>
    >> process = Runtime.getRuntime().exec(cmd);
    >>
    >> PrintWriter pr = new
    >> PrintWriter(process.getOutputStream());
    >> pr.print("username"); //print username to process
    >> pr.print("password"); //print password to process
    >>
    >> This doesn't seems to work. Anybody has experience with this?
    >>
    >> Thanks in advance !!
    >>
    >> Wilson Chew
    >>
    >>

    >
    > It's a start, but you have a way to go. You will also need to use the
    > stdout and
    > stderr streams from the client and read the messages returned from the
    > server.
    > Failing to do so may result in a hang because the Process's output buffers
    > are full.
    >
    > The immediate reaon this doesn't log in is that you forgot to send the
    > enter
    > key after the name and password. Try:
    > pr.print(username + "\r");
    > or:
    > pr.println(username);
    >
    > Steve
    marcus, Aug 1, 2004
    #8
  9. Wilson Chew

    Chris Uppal Guest

    marcus wrote:

    > incorrect -- the reason it doesn't work is because he failed to complete
    > the protocol handshake.


    Please read the post again. For your convenience, here is the code from the
    original post:

    ===========
    String[] cmd = new String[3];

    cmd[0] = "cmd.exe";
    cmd[1] = "/c";
    cmd[2] = "telnet unixMachine";

    process = Runtime.getRuntime().exec(cmd);

    PrintWriter pr = new PrintWriter(process.getOutputStream());
    pr.print("username"); //print username to process
    pr.print("password"); //print password to process
    ===========

    You will note that he is /not/ writing anything (directly) to the network, he
    is /not/ dealing with the telnet protocol, that is done (or would be done if it
    worked) by the already existing telnet /program/.

    -- chris
    Chris Uppal, Aug 1, 2004
    #9
  10. Wilson Chew wrote:
    > hi,
    >
    > Say I am using a Windows machine, can I use Java to programmatically telnet
    > to a unix machine?
    > Here's what I did:
    >
    > String[] cmd = new String[3];
    >
    > cmd[0] = "cmd.exe";
    > cmd[1] = "/c";
    > cmd[2] = "telnet unixMachine";
    >
    > process = Runtime.getRuntime().exec(cmd);
    >
    > PrintWriter pr = new PrintWriter(process.getOutputStream());
    > pr.print("username"); //print username to process
    > pr.print("password"); //print password to process
    >
    > This doesn't seems to work. Anybody has experience with this?
    >
    > Thanks in advance !!
    >
    > Wilson Chew
    >
    >


    You may also want to look up a package called jexpect,
    http://www.joot.com/software/jexpect/

    This may save you a lot of re-inventing wheels.

    Steve
    Steve Horsley, Aug 1, 2004
    #10
  11. Wilson Chew

    rzaks

    Joined:
    Aug 15, 2011
    Messages:
    1
    Another Pet Peeve

    rzaks, Aug 15, 2011
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Van Glass

    Article - Telnet using Java

    Van Glass, Jul 3, 2003, in forum: Java
    Replies:
    0
    Views:
    2,544
    Van Glass
    Jul 3, 2003
  2. Chandrashekar Tippur

    JAVA API's for ssh and telnet

    Chandrashekar Tippur, Dec 2, 2003, in forum: Java
    Replies:
    2
    Views:
    6,276
    Chandrashekar Tippur
    Dec 4, 2003
  3. Kevin Hsu

    mud(telnet) connection using Java

    Kevin Hsu, Apr 11, 2004, in forum: Java
    Replies:
    3
    Views:
    2,420
    steve
    Apr 15, 2004
  4. Jim Isaacson
    Replies:
    5
    Views:
    592
    Default User
    Nov 5, 2004
  5. Carcarius
    Replies:
    0
    Views:
    270
    Carcarius
    Dec 6, 2007
Loading...

Share This Page