Trouble using telentlib

Eddie Corns eddie at holyrood.ed.ac.uk
Mon Feb 28 09:51:31 EST 2005


nitinzep at yahoo.com (Nitin Chaumal) writes:

>I sarched the existing threads but didnt find an answer to this.

>I am writing simple script which uses telentlib to open a session with
>a unix machine and run "tail -f logfile.txt" on one of the logfiles.

>import telnetlib

>HOST = "192.X.X.X"
>user = "myname"
>password = "mypass"

>tn = telnetlib.Telnet(HOST)

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

>tn.write("tail -f /tmp/logfile.txt\n")

># what do i write here ? #

>tn.write("exit\n")

>I want to read every line of the output into a string and run regex on
>it.
>I tried tn.interact() which does show me the ouput but the CPU usage
>on my win2k machine reaches 99% !! :(

>How do i execute a command and then read its output, then another
>command and read its output and so on. I tried to find docs on
>telnetlib but in vain.

>Can somebody guide me please

>Thanks
>Nitin

tn.write("tail -f /tmp/logfile.txt\n")
while True:
  # get one more line
  line = tn.read_until ('\n')
  regex_thing (line)

Remember, "tail -f" never exits, so you probably want to break when you've
find what you want and then just close the telnet session.  It would probably
be nice if telnetlib had a readline function that handled all the messy
details for portable end of line checking.

Eddie



More information about the Python-list mailing list