A python telnet entry level question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jinming Xu

    A python telnet entry level question

    Hello Everyone,

    I am trying to write a python script to telnet to a server and then do
    something there. As the first step, I practiced the python example in Lib
    Reference 11.13.2. But I am finding the script stops after I supplied the
    password. Does anyone know why?


    Thanks in advance!

    Jinming Xu

    PS: Here is the script:

    import getpass
    import sys
    import telnetlib

    HOST = "localhost"
    user = raw_input("Ente r your remote account: ")
    password = getpass.getpass ()

    tn = telnetlib.Telne t(HOST)

    tn.read_until(" login: ")
    tn.write(user + "\n")
    if password:
    tn.read_until(" Password: ")
    tn.write(passwo rd + "\n")

    tn.write("ls\n" )
    tn.write("exit\ n")

    print tn.read_all()

    _______________ _______________ _______________ _______________ _____
    MSN Toolbar provides one-click access to Hotmail from any Web page – FREE
    download! http://toolbar.msn.com/go/onm00200413ave/direct/01/


  • Andrew Jones

    #2
    Re: A python telnet entry level question

    It might be getting stuck on the Password part.

    Try this:

    tn.read_until(" Password: ", 1)


    Ethereal can be very helpful. Use it to see what is happening on the
    wire. It has a "follow TCP stream" function. Obviously this will not
    work for telneting to localhost so you will need to telnet to a remote host.

    Good luck.
    Andy

    Jinming Xu wrote:[color=blue]
    > Hello Everyone,
    >
    > I am trying to write a python script to telnet to a server and then do
    > something there. As the first step, I practiced the python example in
    > Lib Reference 11.13.2. But I am finding the script stops after I
    > supplied the password. Does anyone know why?
    >
    >
    > Thanks in advance!
    >
    > Jinming Xu
    >
    > PS: Here is the script:
    >
    > import getpass
    > import sys
    > import telnetlib
    >
    > HOST = "localhost"
    > user = raw_input("Ente r your remote account: ")
    > password = getpass.getpass ()
    >
    > tn = telnetlib.Telne t(HOST)
    >
    > tn.read_until(" login: ")
    > tn.write(user + "\n")
    > if password:
    > tn.read_until(" Password: ")
    > tn.write(passwo rd + "\n")
    >
    > tn.write("ls\n" )
    > tn.write("exit\ n")
    >
    > print tn.read_all()
    >
    > _______________ _______________ _______________ _______________ _____
    > MSN Toolbar provides one-click access to Hotmail from any Web page –
    > FREE download! http://toolbar.msn.com/go/onm00200413ave/direct/01/
    >
    >[/color]

    Comment

    • Eddie Corns

      #3
      Re: A python telnet entry level question

      "Jinming Xu" <cybermanxu@hot mail.com> writes:
      [color=blue]
      >Hello Everyone,[/color]
      [color=blue]
      >I am trying to write a python script to telnet to a server and then do
      >something there. As the first step, I practiced the python example in Lib
      >Reference 11.13.2. But I am finding the script stops after I supplied the
      >password. Does anyone know why?[/color]

      [color=blue]
      >Thanks in advance![/color]
      [color=blue]
      >Jinming Xu[/color]
      [color=blue]
      >PS: Here is the script:[/color]
      [color=blue]
      >import getpass
      >import sys
      >import telnetlib[/color]
      [color=blue]
      >HOST = "localhost"
      >user = raw_input("Ente r your remote account: ")
      >password = getpass.getpass ()[/color]
      [color=blue]
      >tn = telnetlib.Telne t(HOST)[/color]
      [color=blue]
      >tn.read_until( "login: ")
      >tn.write(use r + "\n")
      >if password:
      > tn.read_until(" Password: ")
      > tn.write(passwo rd + "\n")[/color]
      [color=blue]
      >tn.write("ls\n ")
      >tn.write("exit \n")[/color]
      [color=blue]
      >print tn.read_all()[/color]

      You are maybe being too specific in what you match for. At least one of my
      machines prompts:

      Pasword for <username>:

      rather than just "Password: ". I tend to match things like

      "ogin" (in case of Login vs login)
      "assword" (will this get filtered out by censoring s/w?)

      Eddie

      Comment

      • Cameron Laird

        #4
        Re: A python telnet entry level question

        In article <c685ob$da5$1@s cotsman.ed.ac.u k>,
        Eddie Corns <eddie@holyrood .ed.ac.uk> wrote:

        Comment

        • Eddie Corns

          #5
          Re: A python telnet entry level question

          claird@lairds.c om (Cameron Laird) writes:
          [color=blue]
          >In article <c685ob$da5$1@s cotsman.ed.ac.u k>,
          >Eddie Corns <eddie@holyrood .ed.ac.uk> wrote:
          > .
          > .
          > .[color=green]
          >>You are maybe being too specific in what you match for. At least one of my
          >>machines prompts:
          >>
          >> Pasword for <username>:
          >>
          >>rather than just "Password: ". I tend to match things like
          >>
          >>"ogin" (in case of Login vs login)
          >>"assword" (will this get filtered out by censoring s/w?)
          >>
          >>Eddie[/color][/color]
          [color=blue]
          >This sort of tolerance can lead to its own problems (though
          >I entirely agree you're right to recommend it). Some logins
          >are so sensitive to timing (in essence) that matching
          >"assword" rather than "assword:" results in the telnetd
          >ignoring the first character or two of response.[/color]
          [color=blue]
          >So what to do? At this level, there is *no* good answer.
          >The most enlightened thought is simply to recognize that
          >telnet forces one into a cascade of heuristic hacks.[/color]

          And then you get unhelpful router manufacturers that put code in to check
          whether passwords are typed too fast (or regularly spaced) and ignore them
          because they're obviously not dealing with a human! Took me ages to figure
          out why my scripts were failing (then about 10 seconds to defeat it).

          Yes, using telnet is more art than science but it's a lot better now than
          before we had expect (for heavy duty jobs) and telnetlib (for simpler jobs).

          Eddie

          Comment

          • Cameron Laird

            #6
            Re: A python telnet entry level question

            In article <c6j2ds$8jc$1@s cotsman.ed.ac.u k>,
            Eddie Corns <eddie@holyrood .ed.ac.uk> wrote:

            Comment

            • Mark

              #7
              Re: A python telnet entry level question

              "Andrew Jones" <ajones@sgi.com > wrote in message
              news:c67vo4$go4 gt$1@fido.engr. sgi.com...[color=blue]
              > It might be getting stuck on the Password part.
              >
              > Try this:
              >
              > tn.read_until(" Password: ", 1)
              >
              >
              > Ethereal can be very helpful. Use it to see what is happening on the
              > wire. It has a "follow TCP stream" function. Obviously this will not
              > work for telneting to localhost so you will need to telnet to a remote[/color]
              host.

              I just discovered telnetlib yesterday and found "set_debuglevel " to be very
              useful to figure out what to read_until.

              -Mark
              [color=blue]
              >
              > Good luck.
              > Andy
              >
              > Jinming Xu wrote:[color=green]
              > > Hello Everyone,
              > >
              > > I am trying to write a python script to telnet to a server and then do
              > > something there. As the first step, I practiced the python example in
              > > Lib Reference 11.13.2. But I am finding the script stops after I
              > > supplied the password. Does anyone know why?
              > >
              > >
              > > Thanks in advance!
              > >
              > > Jinming Xu
              > >
              > > PS: Here is the script:
              > >
              > > import getpass
              > > import sys
              > > import telnetlib
              > >
              > > HOST = "localhost"
              > > user = raw_input("Ente r your remote account: ")
              > > password = getpass.getpass ()
              > >
              > > tn = telnetlib.Telne t(HOST)
              > >
              > > tn.read_until(" login: ")
              > > tn.write(user + "\n")
              > > if password:
              > > tn.read_until(" Password: ")
              > > tn.write(passwo rd + "\n")
              > >
              > > tn.write("ls\n" )
              > > tn.write("exit\ n")
              > >
              > > print tn.read_all()
              > >
              > > _______________ _______________ _______________ _______________ _____
              > > MSN Toolbar provides one-click access to Hotmail from any Web page –
              > > FREE download! http://toolbar.msn.com/go/onm00200413ave/direct/01/
              > >
              > >[/color]
              >[/color]


              Comment

              Working...