telnetlib problems

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • vercingetorix52@yahoo.com

    #1

    telnetlib problems

    I'm trying to use a python script to access an embedded computer
    running linux and connected via a crossover ethernet cable using the
    following script...

    ....and I realize the username and password is not realistic... I'm
    still in "proof of concept" stage here :)

    ############### ##########
    import telnetlib

    tn = telnetlib.Telne t('192.168.100. 11')

    tn.read_until(' login: ', 5)

    tn.write('user\ n')

    tn.read_until(' Password: ', 5)

    tn.write('passw ord\n')

    tn.read_until(' bash-2.05$ ', 5)

    tn.write('ls\n' )

    print tn.read_very_ea ger()
    ############### ##########

    As a script, this doesn't work. However, if I execute the same
    commands interactively, it works fine. If I insert some time delays as
    follows...

    ############### ##########
    import telnetlib
    import time

    tn = telnetlib.Telne t('192.168.100. 11')

    tn.read_until(' login: ', 5)
    time.sleep(2)
    tn.write('user\ n')

    tn.read_until(' Password: ', 5)
    time.sleep(2)
    tn.write('passw ord\n')

    tn.read_until(' bash-2.05$ ', 5)

    tn.write('ls\n' )
    time.sleep(2)
    print tn.read_very_ea ger()
    ############### ##########

    ....and it works fine. Can anyone tell me what's going on here? TIA

  • vercingetorix52@yahoo.com

    #2
    Re: telnetlib problems

    I just hit upon something that seems to work...

    ############### ###########
    import telnetlib
    from select import select

    tn = telnetlib.Telne t('192.168.100. 11')
    sock = tn.get_socket()

    tn.read_until(' login: ', 5)
    select([sock], [], [], 5)
    tn.write('user\ n')

    tn.read_until(' Password: ', 5)
    select([sock], [], [], 5)
    tn.write('passw ord\n')

    tn.read_until(' bash-2.05$ ', 5)
    tn.write('ls\n' )
    select([sock], [], [], 5)
    print tn.read_very_ea ger()
    ############### ###########

    If anyone sees any potential problems with this, I would appreciate it.
    TIA

    Comment

    • Eddie Corns

      #3
      Re: telnetlib problems

      vercingetorix52 @yahoo.com writes:
      [color=blue]
      >I'm trying to use a python script to access an embedded computer
      >running linux and connected via a crossover ethernet cable using the
      >following script...[/color]
      [color=blue]
      >...and I realize the username and password is not realistic... I'm
      >still in "proof of concept" stage here :)[/color]
      [color=blue]
      >############## ###########
      >import telnetlib[/color]
      [color=blue]
      >tn = telnetlib.Telne t('192.168.100. 11')
      >tn.read_until( 'login: ', 5)
      >tn.write('user \n')
      >tn.read_until( 'Password: ', 5)
      >tn.write('pass word\n')
      >tn.read_until( 'bash-2.05$ ', 5)
      >tn.write('ls\n ')
      >print tn.read_very_ea ger()[/color]

      [color=blue]
      >As a script, this doesn't work. However, if I execute the same
      >commands interactively, it works fine. If I insert some time delays as
      >follows...[/color]

      What doesn't work about it? Have you checked the return value from the
      read_until()s to see if they're returning anything sensible? are any of them
      timing out?

      Anyway, my first guess would be the use of read_very_eager (), it's something
      you normally only descend to when you're stuck for something to match on.
      Normally you would do another read_until() for the prompt.

      Eddie

      Comment

      • vercingetorix52@yahoo.com

        #4
        Re: telnetlib problems

        Thanks for the reply. I've replaced the call to read_very_eager () with
        read_until() and enabled debugging messages. My script now looks like
        this...

        ############### ##############
        import telnetlib

        tn = telnetlib.Telne t('192.168.100. 11')

        tn.set_debuglev el(9)

        tn.read_until(' login: ', 5)

        tn.write('user\ n')

        tn.read_until(' Password: ', 5)

        tn.write('passw ord\n')

        tn.read_until(' bash-2.05$ ', 5)

        tn.write('ls\n' )

        print tn.read_until(' bash-2.05$ ', 5)
        ############### ##############

        Each call to read_until() returns a valid string except for the second
        one, i.e.,
        tn.read_until(' Password: ', 5) returns a null string.

        Here's the program output with debugging enabled...
        ############### ##############
        Telnet(192.168. 100.11,23): recv "\xff\xfd\x18\x ff\xfd
        \xff\xfd#\xff\x fd'"
        Telnet(192.168. 100.11,23): IAC DO 24
        Telnet(192.168. 100.11,23): IAC DO 32
        Telnet(192.168. 100.11,23): IAC DO 35
        Telnet(192.168. 100.11,23): IAC DO 39
        Telnet(192.168. 100.11,23): recv
        '\xff\xfb\x03\x ff\xfd\x01\xff\ xfd\x1f\xff\xfb \x05\xff\xfd!'
        Telnet(192.168. 100.11,23): IAC WILL 3
        Telnet(192.168. 100.11,23): IAC DO 1
        Telnet(192.168. 100.11,23): IAC DO 31
        Telnet(192.168. 100.11,23): IAC WILL 5
        Telnet(192.168. 100.11,23): IAC DO 33
        Telnet(192.168. 100.11,23): recv '\xff\xfb\x03'
        Telnet(192.168. 100.11,23): IAC WILL 3
        Telnet(192.168. 100.11,23): recv '\xff\xfb\x01Em bedded Systems, 2050
        Single Board Computer.\r\nR'
        Telnet(192.168. 100.11,23): IAC WILL 1
        Telnet(192.168. 100.11,23): recv 'uning \\s Kernel \\r.\r\nEmbedde d:
        2050 Special Feature'
        Telnet(192.168. 100.11,23): recv 's Enabled.\r\n\r\ n'
        Telnet(192.168. 100.11,23): recv 'login: '
        Telnet(192.168. 100.11,23): send 'user\n'
        Telnet(192.168. 100.11,23): send 'password\n'
        Telnet(192.168. 100.11,23): recv 'Password: '
        Telnet(192.168. 100.11,23): send 'ls\n'
        Telnet(192.168. 100.11,23): recv '\r\n'
        Telnet(192.168. 100.11,23): recv 'Login incorrect\r\n\r \nlogin: '


        Login incorrect



        login:
        ############### ##############
        It looks like it's sending the password before receiving the password
        prompt.

        Any ideas?

        Comment

        • Eddie Corns

          #5
          Re: telnetlib problems

          vercingetorix52 @yahoo.com writes:
          [color=blue]
          >Thanks for the reply. I've replaced the call to read_very_eager () with
          >read_until() and enabled debugging messages. My script now looks like
          >this...[/color]
          [color=blue]
          >############## ###############
          >import telnetlib[/color]
          [color=blue]
          >tn = telnetlib.Telne t('192.168.100. 11')[/color]
          [color=blue]
          >tn.set_debugle vel(9)[/color]
          [color=blue]
          >tn.read_until( 'login: ', 5)[/color]
          [color=blue]
          >tn.write('user \n')[/color]
          [color=blue]
          >tn.read_until( 'Password: ', 5)[/color]
          [color=blue]
          >tn.write('pass word\n')[/color]
          [color=blue]
          >tn.read_until( 'bash-2.05$ ', 5)[/color]
          [color=blue]
          >tn.write('ls\n ')[/color]
          [color=blue]
          >print tn.read_until(' bash-2.05$ ', 5)
          >############## ###############[/color]
          [color=blue]
          >Each call to read_until() returns a valid string except for the second
          >one, i.e.,
          >tn.read_until( 'Password: ', 5) returns a null string.[/color]
          [color=blue]
          >Here's the program output with debugging enabled...
          >############## ###############
          >Telnet(192.168 .100.11,23): recv "\xff\xfd\x18\x ff\xfd
          >\xff\xfd#\xff\ xfd'"
          >Telnet(192.168 .100.11,23): IAC DO 24
          >Telnet(192.168 .100.11,23): IAC DO 32
          >Telnet(192.168 .100.11,23): IAC DO 35
          >Telnet(192.168 .100.11,23): IAC DO 39
          >Telnet(192.168 .100.11,23): recv
          >'\xff\xfb\x03\ xff\xfd\x01\xff \xfd\x1f\xff\xf b\x05\xff\xfd!'
          >Telnet(192.168 .100.11,23): IAC WILL 3
          >Telnet(192.168 .100.11,23): IAC DO 1
          >Telnet(192.168 .100.11,23): IAC DO 31
          >Telnet(192.168 .100.11,23): IAC WILL 5
          >Telnet(192.168 .100.11,23): IAC DO 33
          >Telnet(192.168 .100.11,23): recv '\xff\xfb\x03'
          >Telnet(192.168 .100.11,23): IAC WILL 3
          >Telnet(192.168 .100.11,23): recv '\xff\xfb\x01Em bedded Systems, 2050
          >Single Board Computer.\r\nR'
          >Telnet(192.168 .100.11,23): IAC WILL 1
          >Telnet(192.168 .100.11,23): recv 'uning \\s Kernel \\r.\r\nEmbedde d:
          >2050 Special Feature'
          >Telnet(192.168 .100.11,23): recv 's Enabled.\r\n\r\ n'
          >Telnet(192.168 .100.11,23): recv 'login: '
          >Telnet(192.168 .100.11,23): send 'user\n'
          >Telnet(192.168 .100.11,23): send 'password\n'
          >Telnet(192.168 .100.11,23): recv 'Password: '
          >Telnet(192.168 .100.11,23): send 'ls\n'
          >Telnet(192.168 .100.11,23): recv '\r\n'
          >Telnet(192.168 .100.11,23): recv 'Login incorrect\r\n\r \nlogin: '[/color]

          [color=blue]
          >Login incorrect[/color]


          [color=blue]
          >login:
          >############## ###############
          >It looks like it's sending the password before receiving the password
          >prompt.[/color]

          It looks more like it's taking longer than 5 seconds to receive the Password:
          prompt so it times out, returns to your code and you send the next string
          anyway but (assuming this is a valid password) the far end might throw away
          any pending input when it finally does send the Password: prompt (otherwise it
          would probably have got back in sync).

          In short you shouldn't go past each stage unless you've verified that you got
          the expected response. I can't actually remember exactly what the responses
          signify but at a minimum I assume the empty string means there's no point in
          continuing because what you expected to see wasn't there so you need to either
          abort or try some recovery process. And also make sure you give it a
          reasonable time to respond.

          Eddie

          Comment

          Working...