473,473 Members | 2,169 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Trouble using telentlib

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
Jul 18 '05 #1
2 2396
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
Jul 18 '05 #2
ni******@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
Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Jeff | last post by:
/* -------------------------------------------------------------------------- Hello, I was experimenting with class templates and specializing member functions and came across a simple problem...
0
by: James Dean | last post by:
I have trouble displaying a bitmap. I have 1bpp information. I also have a command telling me what color i need to set the bytes to when the colour is switched on. The trouble is i do all this but...
1
by: Jim Bancroft | last post by:
Hi everyone, I'm running into a problem with my ASP.Net application. I've just created a new aspx page which uses some new components of mine that inherit from ServicedComponent and are...
6
by: Daniel Walzenbach | last post by:
Hi, I have a web application which sometimes throws an “out of memory” exception. To get an idea what happens I traced some values using performance monitor and got the following values (for...
1
by: Herv Piedvache | last post by:
Dear all, I have a trouble with the pam authentification for PostgreSQL. I have add in the pg_hba.conf the good line ... and I have create a /etc/pam.d/postresql file which contains : auth ...
1
by: yucikala | last post by:
Hello, I'm a "expert of beginner" in C#. I have a dll - in C. And in this dll is this struct: typedef struct msg_s { /* please make duplicates of strings before next call to emi_read() ! */ ...
1
by: sndive | last post by:
i have a lot of trouble selling twisted a a client lib for network access (on embedded platform) the group i'm a member of wants to write some unmaintainable threaded blocking junk in c--. does...
9
by: itdevries | last post by:
Hi, I've ran into some trouble with an overloaded + operator, maybe someone can give me some hints what to look out for. I've got my own custom vector class, as a part of that I've overloaded...
0
by: mrchatgroup | last post by:
news from http://www.mrchat.net/myblog/myblog/small-accidents-mean-big-trouble-for-supercollider.html Small Accidents Mean Big Trouble for Supercollider Image Scientists expect startup...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.