Foundations of Python Network Programming

(WallPaper) #1
Chapter 16 ■ telnet and SSh

301

args = parser.parse_args()
password = getpass.getpass('Password: ')
main(args.hostname, args.username, password)


If the script is successful, it shows you what the simple uptime command prints on the remote system.

$ python telnet_login.py example.com brandon
Password: abc123
10:24:43 up 5 days, 12:13, 14 users, load average: 1.44, 0.91, 0.73


The listing shows you the general structure of a session powered by telnetlib. First, a connection is established
that is represented in Python by an instance of the Telnet class. Here only the hostname is specified, though you can
also provide a port number to connect to some other service port than standard Telnet.
You can call set_debuglevel(1) if you want your Telnet object to print out all of the strings that it sends and
receives during the session. This actually turned out to be important for writing even the very simple script shown in
the listing, because in two different cases the script hung and I had to rerun it with debugging messages turned on
so that I could see the actual output and fix the script. (Once I failed to match the exact text that was coming back,
and the other time I forgot the '\r' at the end of the uptime command.) I generally turn off debugging only once a
program is working perfectly and then turn it back on whenever I want to do more work on the script.
Note that Telnet does not disguise the fact that its service is backed by a TCP socket, and it will pass through to
your program any socket.error or socket.gaierror exceptions that are raised.
Once the Telnet session is established, interaction generally falls into a receive-and-send pattern, where you wait
for a prompt or response from the remote end and then send your next piece of information. The listing illustrates two
methods of waiting for text to arrive:


•    The simple read_until() method watches for a literal string to arrive, and then it returns
a string that provides all the text that it received from the moment it started listing until the
moment it finally saw the string you were waiting for.

•    The more powerful and sophisticated expect() method takes a list of Python regular
expressions. Once the text arriving from the remote end finally adds up to something that
matches one of the regular expressions, expect() returns three items: the index in your list
of the pattern that matched, the regular expression SRE_Match object itself, and the text that
was received leading up to the matching text. For more information on what you can do with
a SRE_Match, including finding the values of any subexpressions in your pattern, read the
Standard Library documentation for the re module.

Regular expressions, as always, have to be written carefully. When I first wrote this script, I used '$' as the
expect() pattern that watched for the shell prompt to appear—which, alas, is a special character in a regular
expression! So, the corrected script shown in the listing escapes the $ so that expect() actually waits until it sees a
dollar sign arrive from the remote end.
If the script sees an error message because of an incorrect password and does not get stuck waiting forever for a
login or password prompt that never arrives or that looks different from it was expecting, it exits.


$ python telnet_login.py example.com brandon
Password: wrongpass
Username and password failed - giving up


If you wind up writing a Python script that has to use Telnet, it will simply be a larger or more complicated
version of the same simple pattern shown here.
Both read_until() and expect() take an optional second argument named timeout that places a maximum
limit on how long in seconds the call will watch for the text pattern before giving up and returning control to your
Python script. If they quit and give up because of the timeout, they do not raise an error; instead (awkwardly enough),
they just return the text they have seen so far and leave it to you to figure out whether that text contains the pattern!

Free download pdf