How to use 'top' using Python

Reply

Join Date: Aug 2005
Posts: 46
Reputation: xav.vijay is an unknown quantity at this point 
Solved Threads: 6
xav.vijay's Avatar
xav.vijay xav.vijay is offline Offline
Light Poster

How to use 'top' using Python

 
0
  #1
Aug 16th, 2005
Hai Friends
This is my first post to this forum. I am new to Python but I find its quite interesting. I have a problem with the folowing code...

##########################
import telnetlib
import sys

HOST = "192.168.10.5"

USER = "somename"
PASSWORD = "somepassword"

telnet = telnetlib.Telnet(HOST)

telnet.read_until("login: ")
telnet.write(USER + "\n")

telnet.read_until("Password: ")
telnet.write(PASSWORD + "\n")

telnet.write("top -p 1 -n 1\n")
telnet.write("exit\n")

some_value = telnet.read_all()
print some_value

#############################

The problem is that, when I print 'some_value' I get
" top: Unknown terminal "network" in $TERM ". But when I use other commands like 'ls -l' or 'df -h' it works fine & gives me the o/p of the commands. I tried by redirecting the o/p to a file like "top -p 1 -n 1 >> test\n" & then reading the contents of the file, but still it doent work. The file created thru Python was empty. When I directly work on Linux, the command works fine. Can anybody tell what the problem is...
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 146
Reputation: G-Do is an unknown quantity at this point 
Solved Threads: 28
G-Do's Avatar
G-Do G-Do is offline Offline
Junior Poster

Re: How to use 'top' using Python

 
0
  #2
Aug 16th, 2005
Hi xav.vijay,

I'm not sure, and I can't check right now because I'm at work and using my XP box, but:

Isn't the difference between top and df/ls that programs like df/ls write to STDOUT once and then terminate, while top keeps writing to the terminal window, dynamically updating the information you see? Maybe top relies upon a C library that needs specific information about the terminal it's writing to, and because we're working in Python that information has been lost? Check your TERM environment variable. Is its value really "network"?

I will try to help you more later on today.
Vi veri veniversum vivus vici
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 46
Reputation: xav.vijay is an unknown quantity at this point 
Solved Threads: 6
xav.vijay's Avatar
xav.vijay xav.vijay is offline Offline
Light Poster

Re: How to use 'top' using Python

 
0
  #3
Aug 17th, 2005
Hai G-Do
Thanks for ur reply. U r exactly right about the 'top' command. It doesnt stop until we type 'q' to stop it. I had this problem, so I gave the command as 'top -p 1 -n 1'. Here the command eliminates all PID's & refreshes only once and comes back to the prompt... So I redirected to the o/p to a file named 'test'. When I do this in Linux, the file 'test' has the exact o/p... But when I do it with Python this is what I get... Here is the full error message I get

top -p 1 -n 6 >> test
cat test
exit
Last login: Mon Aug 15 16:57:18 from 10.128.17.34
unknown terminal "network"
unknown terminal "network"
bash: /bin/chmod: Permission denied
[scb@inmaarfesweb2 scb]$ top -p 1 -n 6 >> test
top: Unknown terminal "network" in $TERM
[scb@inmaarfesweb2 scb]$ cat test
[scb@inmaarfesweb2 scb]$ exit
logout
'network': unknown terminal type.


Now after executing the program when I go into Linux & open the file 'test', its empty... I dont know why??

Now I am having a real bad time here... Even the programs which were working for me regularly till yesterday aren't working now...I get the following error message...

Traceback (most recent call last):
File "F:\xav\VM_Calc.py", line 9, in ?
xlApp.Workbooks.open("F:/xav/cpu&virtual.xls")
File "<COMObject <unknown>>", line 2, in open
com_error: (-2147352567, 'Exception occurred.', (0, 'Microsoft Excel', "'cpu&virtual.xls' could not be found. Check the spelling of the file name, and verify that the file location is correct.\n\nIf you are trying to open the file from your list of most recently used files on the File menu, make sure that the file has not been renamed, moved, or deleted.", 'D:\\Program Files\\Microsoft Office\\Office\\1033\\xlmain9.chm', 0, -2146827284), None)


I checked out the path & filename ...But still it is not working... In fact they were working fine for me till yesterday... Is there anything I should do???
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 146
Reputation: G-Do is an unknown quantity at this point 
Solved Threads: 28
G-Do's Avatar
G-Do G-Do is offline Offline
Junior Poster

Re: How to use 'top' using Python

 
0
  #4
Aug 17th, 2005
Hi, xav.vijay,

I couldn't get your original code to work, and I'm hesitant to play with it, seeing as how it's telnet, and telnet is unsecured.

I am able to do this:
  1. import sys, os
  2. f = sys.popen("top -p 1 -n 1", "r")
  3. text = f.read()
  4. print text
This prints the top output to screen. Perhaps you could collect the output this way, then send it to telnet.write()?

Also, what is the value of $TERM in your shell?
Vi veri veniversum vivus vici
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 146
Reputation: G-Do is an unknown quantity at this point 
Solved Threads: 28
G-Do's Avatar
G-Do G-Do is offline Offline
Junior Poster

Re: How to use 'top' using Python

 
0
  #5
Aug 17th, 2005
Hi again,

You also might want to consider using the -b, or batch option for top:
b Batch mode. Useful for sending output from top to other programs or to a file. In this mode, top will not accept command line input. It runs until it produces the number of iterations requested with the n option or until killed. Output is plain text suitable for display on a dumb terminal.
Ripped from here.
Vi veri veniversum vivus vici
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 215
Reputation: shanenin is an unknown quantity at this point 
Solved Threads: 16
shanenin shanenin is offline Offline
Posting Whiz in Training

Re: How to use 'top' using Python

 
0
  #6
Aug 17th, 2005
this is not probably helpful. but for getting ouput. I like to use the commands.getoutput function. After you get the output, you could write it to a file
  1. >>> import commands
  2. >>> output = commands.getoutput('top -p 1 -n 1')
  3. >>> print output
  4.  
  5. top - 12:15:11 up 2:00, 2 users, load average: 0.18, 0.25, 0.21
  6. Tasks: 1 total, 0 running, 1 sleeping, 0 stopped, 0 zombie
  7. Cpu(s): 12.5% us, 2.2% sy, 5.5% ni, 78.8% id, 0.7% wa, 0.3% hi, 0.0% si
  8. Mem: 515720k total, 287572k used, 228148k free, 7352k buffers
  9. Swap: 506036k total, 0k used, 506036k free, 174364k cached
  10.  
  11. PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
  12. 1 root 16 0 1388 508 1236 S 0.0 0.1 0:00.58 init
In a perfect world exceptions would not be needed.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 46
Reputation: xav.vijay is an unknown quantity at this point 
Solved Threads: 6
xav.vijay's Avatar
xav.vijay xav.vijay is offline Offline
Light Poster

Re: How to use 'top' using Python

 
0
  #7
Aug 19th, 2005
Hai friends
Sorry for the late reply, But thanks for ur suggestions... and really a big thanks to u G-do... the '-b' option worked well for me...
But I had problems with the codes u gave me... as for the one given by G-do, I got the following error message...in Windows

Traceback (most recent call last):
File "F:\xav\space1.py", line 53, in ?
f = sys.popen("top -p 1 -n 1", "r")
AttributeError: 'module' object has no attribute 'popen'


and the following error message in Linux...

Traceback (innermost last):
File "<stdin>", line 1, in ?
AttributeError: popen


I dont know what I should do to add the 'popen' module ...

But Shanenin's code worked really well in linux...But in windows it said
>>> import commands
>>> output = commands.getoutput('dir')
>>> print output
'{' is not recognized as an internal or external command,
operable program or batch file.
>>>

I dont know why it took '{' for '('....

Well G-do & Shanenin I would like ask u something... wont ur codes pick the information of the computer's which
python is currently running?? How do we access them for a remote server???

And G-do... how do I find the value of $TERM, I think I got 'network' from the error messages...
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,003
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 928
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: How to use 'top' using Python

 
0
  #8
Aug 19th, 2005
The module commands only works in Linux and popen() is in module os that would explain few things. Otherwise I don't know nothing!
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 215
Reputation: shanenin is an unknown quantity at this point 
Solved Threads: 16
shanenin shanenin is offline Offline
Posting Whiz in Training

Re: How to use 'top' using Python

 
0
  #9
Aug 19th, 2005
to access your info for a remote computer, you could log in using ssh. The remote computer would also need python. I am not sure if this is what you are asking.
  1. shane@mainbox shane $ ssh 192.168.1.104
  2. Password:
  3. Last login: Sun Jul 31 19:58:05 2005 from 192.168.1.105
  4. shane@freevo shane $ python
  5. Python 2.3.3 (#1, Oct 3 2004, 15:43:03)
  6. [GCC 3.3.4 20040623 (Gentoo Linux 3.3.4-r1, ssp-3.3.2-2, pie-8.7.6)] on linux2
  7. Type "help", "copyright", "credits" or "license" for more information.
  8. >>> import commands
  9. >>> uname_output = commands.getoutput('uname -a')
  10. >>> print uname_output
  11. Linux freevo 2.6.8-gentoo-r3 #3 Mon May 16 22:25:23 CDT 2005 i686 Intel(R) Celeron(R) CPU 2.00GHz GenuineIntel GNU/Linux
In a perfect world exceptions would not be needed.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 146
Reputation: G-Do is an unknown quantity at this point 
Solved Threads: 28
G-Do's Avatar
G-Do G-Do is offline Offline
Junior Poster

Re: How to use 'top' using Python

 
0
  #10
Aug 19th, 2005
Hi xav.vijay,

First things first: I made a mistake. Vegaseat is correct, the popen function is in the os module, not the sys module. The corrected code looks like:
  1. import sys, os
  2. f = os.popen("top -p 1 -n 1", "r")
  3. text = f.read()
  4. print text
To get the value of any environment variable, such as PATH or TERM, you type:
  1. echo $VARIABLE_NAME
in Linux (bash shell), or:
  1. echo %VARIABLE_NAME%
in Windows. Of course, I think TERM is usually a Linux-specific environment variable, so if you try that in Windows, you're not going to get anything useful. I want to know the value of TERM both on your local machine and on the remote server, because someone (the telnet daemon on the remote server, I suspect) is telling top that the terminal type is "network," and while this is accurate, we may be able to trick it into thinking it is "xterm" which should make it work without any negative consequences, provided you are using the -b option.

Now, for the meat. Let me see if I understand: you are trying to write a telnet program that logs into a remote server, executes `top` on that server, and prints the output on the local machine, correct? So you need two things: a way to execute top remotely, and a way to collect the output. Have you tried using the -b option with your original code? In other words, I need to know if the following works for you:
  1. import sys, telnetlib
  2. # -- Hard-code our host IP, our username, and our password
  3. HOST = "192.168.10.5"
  4. USER = "somename"
  5. PASSWORD = "somepassword"
  6. # -- Instantiate our telnet client and connect to the remote server
  7. telnet = telnetlib.Telnet(HOST)
  8. # -- Tell the telnet daemon our username and password
  9. telnet.read_until("login: ")
  10. telnet.write(USER + "\n")
  11. telnet.read_until("Password: ")
  12. telnet.write(PASSWORD + "\n")
  13. # -- Print the `top -p 1 -n 1 -b` output
  14. telnet.write("top -p 1 -n 1 -b\n")
  15. # Unsure if we should exit before read_all(), maybe uncomment this?
  16. # telnet.write("exit\n")
  17. # -- Read all the text from the telnet client, print it
  18. print telnet.read_all()
If not, try uncommenting the line which causes telnet to exit, then run it again. Does this method give any output at all?

I would like to test the code myself, but telnet is not a secure remote-login protocol, and I don't have any telnet-able accounts on any servers I know of.

EDIT: Also, you might consider using getpass to get your password, rather than hard-coding it into the file:
  1. import getpass
  2. password = getpass.getpass()
  3. print password
Try it!
Vi veri veniversum vivus vici
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Python Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC