Foundations of Python Network Programming

(WallPaper) #1

Chapter 16 ■ telnet and SSh


290



  1. Ansible is a sleek and powerful system that lets you declare how dozens or hundreds of
    remote machines should be configured. It connects to each one of them with SSH, and
    it performs whatever checks or updates are necessary. Its speed and design have drawn
    attention not only from the Python community but from the system administration
    discipline at large (see http://docs.ansible.com/index.html)..)

  2. SaltStack makes you install its own agent on each client machine instead of simply riding
    atop SSH. This allows the master to push new information to other machines much
    more quickly than would be possible over hundreds or thousands of simultaneous SSH
    connections. In return, it is blazingly fast, even for huge installations and large clusters
    (see http://www.saltstack.com/)..)


Finally, I should mention pexpect. While technically it is not a program that knows how to use the network, it is
often used to control the system ssh or telnet command when a Python programmer wants to automate interactions
with a remote prompt of some kind. This typically takes place in a situation where no API is available for a device
and commands simply have to be typed each time the command-line prompt appears. Configuring simple network
hardware often requires this kind of clunky step-by-step interaction. You can learn more about pexpect at
http://pypi.python.org/pypi/pexpect.
Of course, it might be that no automated solution like these will quite suffice for your project, and you will
actually have to roll up your sleeves and learn how to manipulate remote-shell protocols yourself. In that case, you
have come to the right place. Keep reading!


Command-Line Expansion and Quoting

If you have ever typed commands at a Unix command prompt, you are aware that not every character you type is
interpreted literally. For example, consider the following command. (Note that in this and all of the examples that
follow in this chapter, I will be using the dollar sign, $, as the shell’s prompt, which tells you “it is your turn to type.”)


$ echo *
sftp.py shell.py ssh_commands.py ssh_simple.py ssh_simple.txt ssh_threads.py telnet_codes.py
telnet_login.py


The asterisk (*) in this command was not interpreted to mean “print an actual asterisk character to the screen.”
Instead, the shell thought I was trying to write a pattern to match all the file names in the current directory. To print a
real asterisk, I have to use another special character, an escape character, because it lets me “escape” from the shell’s
normal meaning to tell it that I just mean the asterisk literally.


$ echo Here is a lone asterisk: *
Here is a lone asterisk: *


$ echo And here are '' two "" more asterisks
And here are two more asterisks


Shells can run subprocesses whose output is then used in the text of yet another command—and they can even
do math nowadays. To figure out how many words per line Neal Stephenson fits in the plain-text version of his “In the
Beginning... Was the Command Line” essay, you can ask the ubiquitous bash Bourne-again shell—the standard shell
on most Linux systems these days—to divide the number of words in the essay by the number of lines and produce
a result.


$ echo $(( $(wc -w < command.txt) / $(wc -l < command.txt) )) words per line
44 words per line

Free download pdf