Regarding Telnet library in python

Hishaam hishaam.ab at gmail.com
Wed Aug 13 08:04:34 EDT 2008


Hi,

In python documentation, i found a telnet example as follows:

-------------------------------------------------------------------------
import getpass
import sys
import telnetlib

HOST = "localhost"
user = raw_input("Enter your remote account: ")
password = getpass.getpass()

tn = telnetlib.Telnet(HOST)

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

tn.write("ls\n")
tn.write("exit\n")

print tn.read_all()
-------------------------------------------------------------------------

The ouput of it as follows:

-------------------------------------------------------------------------
Enter your remote account: root

Last login: Mon Aug 13 11:54:32 from pcp246879pcs.ca
Sun Microsystems Inc.   SunOS 5.10      Generic January 2005
# Desktop     boot        hishaam     net         system      work
Documents   cdrom       home        opt         tfile2      zonemgr
File.txt    dev         kernel      platform    tmp
QA_tmp      devices     lib         proc        usr
acunix      etc         lost+found  sbin        var
bin         export      mnt         sfile       vol
#

-------------------------------------------------------------------------

The difficulty i find in this is th code line "print tn.read_all()" is
used for reading all of the output of the code at once.

Is there a possibility to read the stdout of each command by command
like -

# ls
<stdout 1>
[capture <stdout 1> in a variable]
# cd /root
<stdout 2>
[capture <stdout 2> in a variable]


Like the above would be useful if there are a large number of commands
to be executed in the telnet session.

Can anyone help on this?

Regards,
Hishaam



More information about the Python-list mailing list