#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    2
    Rep Power
    0

    Write in to telnet session (noob)


    Hi, I need to get a connection to a device via TELNET (i use IP2COM cause i want to see what i does behind the scenes) and then write some commands.

    i'm doing it in Eclipse and getting the error message below ->
    File "C:\Users\user\workspace\main\src\telnet.py", line 23

    ^
    SyntaxError: unexpected EOF while parsing

    The strange thing is that i have only 22 lines.. line number 23 is empty...

    Can someone help me with that?

    Thanks.

    Here is the code that im using:

    Code:
    import getpass
    import sys
    import telnetlib
    
    HOST = "172.17.174.50"
    port = "1003"
    #user = input("Enter your remote account: ")
    #password = getpass.getpass()
    
    tn = telnetlib.Telnet(HOST, port)
    
    #tn.read_until("user:")
    #tn.write(user.encode('ascii') + b"\r")
    
    #tn.write(user.encode("test" + "\r")
    
    #if password:
    #    tn.read_until(b"Password: ")
    #   tn.write(password.encode('ascii') + b"\n")
    tn.write("sh run" + "\r")
    tn.write("exit" + "\r")
    print(tn.read_all()
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Location
    Canada
    Posts
    51
    Rep Power
    2
    Originally Posted by nickaf
    Hi, I need to get a connection to a device via TELNET (i use IP2COM cause i want to see what i does behind the scenes) and then write some commands.

    i'm doing it in Eclipse and getting the error message below ->
    File "C:\Users\user\workspace\main\src\telnet.py", line 23

    ^
    SyntaxError: unexpected EOF while parsing

    The strange thing is that i have only 22 lines.. line number 23 is empty...

    Can someone help me with that?

    Thanks.

    Here is the code that im using:

    Code:
    import getpass
    import sys
    import telnetlib
    
    HOST = "172.17.174.50"
    port = "1003"
    #user = input("Enter your remote account: ")
    #password = getpass.getpass()
    
    tn = telnetlib.Telnet(HOST, port)
    
    #tn.read_until("user:")
    #tn.write(user.encode('ascii') + b"\r")
    
    #tn.write(user.encode("test" + "\r")
    
    #if password:
    #    tn.read_until(b"Password: ")
    #   tn.write(password.encode('ascii') + b"\n")
    tn.write("sh run" + "\r")
    tn.write("exit" + "\r")
    print(tn.read_all()
    I haven't tested it so there might be other errors but on the last line you need one more closing paretheses at the end.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    2
    Rep Power
    0
    Originally Posted by Good News
    I haven't tested it so there might be other errors but on the last line you need one more closing paretheses at the end.
    Yes, that's true but after i fixed that still can't write to the telnet session here is the new error..

    Code:
    Traceback (most recent call last):
      File "C:\Users\user\workspace\main\src\telnet2.py", line 20, in <module>
        tn.write("sh run" + "\r")
      File "c:\python33\lib\telnetlib.py", line 279, in write
        if IAC in buffer:
    TypeError: 'in <string>' requires string as left operand, not bytes
    P.S checked with putty the session is ok and allowed multiple connections.

    Thanks,

IMN logo majestic logo threadwatch logo seochat tools logo