Trouble using telentlib

N

Nitin Chaumal

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
 
K

Kartic

Nitin Chaumal said the following on 2/11/2005 5:41 PM:
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")

Nitin,

You can not have two writes together. Reads and writes should alternate.

To get the log file this is what you should do:

# Modified my working version for your purpose.
# Hope there are not errors!

def SaveLog(user, password):
telnet_resp = []
tn = telnetlib.Telnet(host)

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


tn.read_until("$ ")
tn.write("tail -f /tmp/logfile.txt\n")
telnet_resp.append(tn.read_until("$ "))
tn.write("exit\n")

telnet_resp.append(tn.read_all() )
tn.close()


telnet_out = open(OUTPUT, 'a')
telnet_out.write('\n'.join(telnet_resp))
telnet_out.close()


Alternatively, if your box has FTP access, you can FTP the log file to a
CStringIO file and operate on it.

Thanks,
-Kartic
 
E

Eddie Corns

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 ? #

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,992
Messages
2,570,220
Members
46,807
Latest member
ryef

Latest Threads

Top