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 = ""

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")

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...

11 Years
Discussion Span
Last Post by xav.vijay
Featured Replies
  • 1

    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: [CODE]import sys, os f = sys.popen("top -p 1 -n 1", "r") text = f.read() print text[/CODE]This prints the … Read More

  • 1

    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: [CODE]import sys, os f = os.popen("top -p 1 -n 1", "r") text = f.read() print text[/CODE] To get the value of … Read More


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.


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
Last login: Mon Aug 15 16:57:18 from
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
'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 ?
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???


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:

import sys, os
f = sys.popen("top -p 1 -n 1", "r")
text = f.read()
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?


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.


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

>>> import commands
>>> output = commands.getoutput('top -p 1 -n 1')
>>> print output

top - 12:15:11 up  2:00,  2 users,  load average: 0.18, 0.25, 0.21
Tasks:   1 total,   0 running,   1 sleeping,   0 stopped,   0 zombie
Cpu(s): 12.5% us,  2.2% sy,  5.5% ni, 78.8% id,  0.7% wa,  0.3% hi,  0.0% si
Mem:    515720k total,   287572k used,   228148k free,     7352k buffers
Swap:   506036k total,        0k used,   506036k free,   174364k cached

    1 root      16   0  1388  508 1236 S  0.0  0.1   0:00.58 init

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:xavspace1.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...

Edited by pyTony: fixed formating


The module commands only works in Linux and popen() is in module os that would explain few things. Otherwise I don't know nothing!


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.

shane@mainbox shane $ ssh
Last login: Sun Jul 31 19:58:05 2005 from
shane@freevo shane $ python
Python 2.3.3 (#1, Oct  3 2004, 15:43:03)
[GCC 3.3.4 20040623 (Gentoo Linux 3.3.4-r1, ssp-3.3.2-2, pie-8.7.6)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import commands
>>> uname_output = commands.getoutput('uname -a')
>>> print uname_output
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

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:

import sys, os
f = os.popen("top -p 1 -n 1", "r")
text = f.read()
print text

To get the value of any environment variable, such as PATH or TERM, you type:


in Linux (bash shell), or:


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:

import sys, telnetlib
# -- Hard-code our host IP, our username, and our password
HOST = ""
USER = "somename"
PASSWORD = "somepassword"
# -- Instantiate our telnet client and connect to the remote server
telnet = telnetlib.Telnet(HOST)
# -- Tell the telnet daemon our username and password
telnet.read_until("login: ")
telnet.write(USER + "\n")
telnet.read_until("Password: ")
telnet.write(PASSWORD + "\n")
# -- Print the `top -p 1 -n 1 -b` output
telnet.write("top -p 1 -n 1 -b\n")
# Unsure if we should exit before read_all(), maybe uncomment this?
# telnet.write("exit\n")
# -- Read all the text from the telnet client, print it
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:

import getpass
password = getpass.getpass()
print password

Try it!


Hi friends
Well G-Do you are absolutly correct, I am logging into a remote Linux server & collecting the information of 'top' to a local machine. When I said that there was a problem with ur code, I meant the code which uses the 'popen' function, because it was picking info of the client which I was currently working on. But as I said the '-b' option is working fine with the original code. Thanks for the tip.... I checked with the value of $TERM & it is 'xterm'.... I tried searching for Windows as well, but I couldnt find it..I dont think there is any 'terminal type' variable for windows??? Thanks for the suggestion for using 'getpass()', it works great... just what I wanted...

And Shanenin the client which I am using is windows, so I need softwares like puTTY to login to Linux via SSH... moreover ur code returns the output to the server itself & not to the client where I use my python program...so how would u retrieve the data to a client PC??

But anyway thanks to ur suggestions guys, now the code works great....

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.