Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old August 13th, 2008, 01:05 PM
Hishaam
Guest
 
Posts: n/a
Default Regarding Telnet library in python

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 1in a variable]
# cd /root
<stdout 2>
[capture <stdout 2in 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
  #2  
Old August 13th, 2008, 03:35 PM
Eddie Corns
Guest
 
Posts: n/a
Default Re: Regarding Telnet library in python

Hishaam <hishaam.ab@gmail.comwrites:
Quote:
>Hi,
Quote:
>In python documentation, i found a telnet example as follows:
Quote:
>-------------------------------------------------------------------------
>import getpass
>import sys
>import telnetlib
Quote:
>HOST = "localhost"
>user = raw_input("Enter your remote account: ")
>password = getpass.getpass()
Quote:
>tn = telnetlib.Telnet(HOST)
Quote:
>tn.read_until("login: ")
>tn.write(user + "\n")
>if password:
tn.read_until("Password: ")
tn.write(password + "\n")
Quote:
>tn.write("ls\n")
>tn.write("exit\n")
Quote:
>print tn.read_all()
>-------------------------------------------------------------------------
Quote:
>The ouput of it as follows:
Quote:
>-------------------------------------------------------------------------
>Enter your remote account: root
Quote:
>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
>#
Quote:
>-------------------------------------------------------------------------
Quote:
>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.
Quote:
>Is there a possibility to read the stdout of each command by command
>like -
Quote:
># ls
><stdout 1>
>[capture <stdout 1in a variable]
># cd /root
><stdout 2>
>[capture <stdout 2in a variable]
Quote:
>Like the above would be useful if there are a large number of commands
>to be executed in the telnet session.
Quote:
>Can anyone help on this?
Quote:
>Regards,
>Hishaam
One way of doing this is to send the commands one at a time and do: (untested)

prompt = '\n# '

tn.write("ls\n")
stdout = tn.read_until(prompt)
for line in stdout.splitlines():
...
etc.

alternatively just send them all in one go as you have been and save the
output in a variable and split that on the prompt. In either case you may
want to change the prompt to something easier to match on like "$hostname# ".

Eddie
 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles