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 6 of 6

Thread: Handle Telnet Session Progmatically

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Angry Handle Telnet Session Progmatically

    Hi there,

    I am trying to write a small application to handle a telnet session on a old cisco device, login, issue commands, write responses to log file. I've done this quite easily in perl, but intend to expand the program quite a bit and am much more comfortable with java so am trying to port it. I don't want to use third party libraries(jscape, apache, etc...)

    However, I am not getting the response Im expecting from the server, where I should be getting a login prompt I'm getting:

    ��������
    and if I change readLine to read
    65533

    The basic method is:
    		Socket socket = new Socket("My_Devices_IP", 23);
    		socket.setKeepAlive(true);
     
    		BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    		BufferedWriter out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
     
    		out.write("admin\r\n");
    		System.out.println(in.readLine());		
     
    		socket.close();

    I must be missing something! Any suggestions welcome.


  2. #2
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Handle Telnet Session Progmatically

    That looks about right. When you use telnet on your old equipment, does it ask you for a username? My router doesn't. You might be seeing some telnet control bytes, followed directly by a password prompt. I suspect unless your application is very complicated you might be able to "wing it" with plain text. On the other hand, the first time you have to tackle a problem with control characters you're going to have to start re-writing one of the popular java telnet libraries.

  3. #3
    Junior Member
    Join Date
    Apr 2012
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Handle Telnet Session Progmatically

    When I telnet to the switch it responds with the hostname and a login prompt... I must be missing something.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Handle Telnet Session Progmatically

    Try reading the response as bytes and then use the Integer class's toHexString() method to print out the bytes one by one in hex to see what you are reading
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Handle Telnet Session Progmatically

    it responds with the hostname and a login prompt
    I suppose there was no great reason why it had to respond the same as my cheap router at home! Have you tried extending your code to send a username *and* the password? If you read the inputstream between writes, you may see less garbled output after the first exchange. If I attempt this with my router using the 'netcat' utility, the first response from the router is garbled (Telnet control bytes? The same integer value 65533 that you reported) and the rest is readable. Depending on how big the input buffer is on your old equipment you may need to delay sending the password until the username is handled, or even implement some handshaking based on reading expected responses. For something as well known as telnet, I think it would be easier to use one of the popular open source solutions. StackOverflow has a relevant page here:

    Looking for Java Telnet emulator - Stack Overflow

  6. #6
    Junior Member
    Join Date
    Apr 2012
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Handle Telnet Session Progmatically

    Thanks for the suggestions, I still haven't worked it out yet but I think I am getting closer.

Similar Threads

  1. [ASK] Telnet and Java
    By ragilsms2746 in forum Java Theory & Questions
    Replies: 1
    Last Post: October 27th, 2011, 05:51 AM
  2. Try : handle an exception by yourself.
    By try in forum Member Introductions
    Replies: 1
    Last Post: July 12th, 2011, 10:37 AM
  3. how to handle many more exceptions
    By shailaja in forum Member Introductions
    Replies: 1
    Last Post: December 27th, 2010, 01:02 AM
  4. Autocomplete over telnet connection
    By April in forum Java Networking
    Replies: 6
    Last Post: June 17th, 2010, 08:58 AM
  5. opening Telnet Command Session
    By voyager in forum Java Networking
    Replies: 3
    Last Post: June 23rd, 2009, 10:34 AM