473,800 Members | 3,070 Online
Bytes | Software Development & Data Engineering Community
+ 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.Telne t(HOST)

tn.read_until(" login: ")
tn.write(user + "\n")
if password:
tn.read_until(" Password: ")
tn.write(passwo rd + "\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 2416
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.Telne t(HOST)

tn.read_until(" login: ")
tn.write(user + "\n")
if password:
tn.read_until(" Password: ")
tn.write(passwo rd + "\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.Telne t(host)

tn.read_until(" login: ")
tn.write(user + "\n")
if password:
telnet_resp.app end( tn.read_until(" Password: ") )
tn.write(passwo rd + "\n")
tn.read_until(" $ ")
tn.write("tail -f /tmp/logfile.txt\n")
telnet_resp.app end(tn.read_unt il("$ "))
tn.write("exit\ n")

telnet_resp.app end(tn.read_all () )
tn.close()
telnet_out = open(OUTPUT, 'a')
telnet_out.writ e('\n'.join(tel net_resp))
telnet_out.clos e()
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.Telne t(HOST) tn.read_until( "login: ")
tn.write(use r + "\n")
if password:
tn.read_until(" Password: ")
tn.write(passwo rd + "\n") tn.write("ta il -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
5780
by: Jeff | last post by:
/* -------------------------------------------------------------------------- Hello, I was experimenting with class templates and specializing member functions and came across a simple problem which I can't figure out. I have a simple class C with 2 template member objects, and a function print() that prints the value of these objects. I defined a specialization of print() for C<string, char> which is called correctly. Then I wanted...
0
1291
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 it will not display properly. I convert this to 8bpp information. I set the relevant pixel to for example "255,0,0" for Red.....is this right?....i set the Red byte to 255 and the other two values set to 0. I think the Bitmap data class i am...
1
2007
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 designed to run under COM+. This page works fine, unless I get fancy and hit my browser's refresh button quickly, as a test. Then, what I see happen is the transactions pile up and my ASP.Net "current requests" do the same. This happens even with two...
6
3810
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 one day): \\FFDS24\ASP.NET Applications(_LM_W3SVC_1_Root_ATV2004)\Errors During Execution: 7 \\FFDS24\ASP.NET Apps v1.1.4322(_LM_W3SVC_1_Root_ATV2004)\Compilations
1
1875
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 required pam_unix.so nullok_secure account required pam_unix.so
1
2150
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() ! */ int op_type; /* of "op_t" type: operation type; submit (>0), response (<0) */
1
1228
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 anyone can give me an idea how many kilobytes extra would it take to have a pyqt linked in and a running qtreactor? (qtreactor natirally depends on the qt bindings) is there an exaample of going thru a passworded proxy
9
2060
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 the + operator by means of a friend function. Everything worked fine until I decided to use a variable array size (by using new/delete), now I get an error when a temporary object is deleted (e.g. after addition), the error occurs at the delete...
0
1679
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 glitches in the massive, complex machines they use to smash atoms.
0
9553
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10509
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10281
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7584
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6824
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5612
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4152
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 we have to send another system
2
3765
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2953
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.