Chapter 16 ■ telnet and SSh
315
Summary
Remote-shell protocols let you connect to remote machines, run shell commands, and see their output, just as
though the commands were running inside a local terminal window. Sometimes you use these protocols to connect
to an actual Unix shell and sometimes to small, embedded shells in routers or other networking hardware that needs
configuring.
As always, when talking to Unix commands, you need to be aware of output buffering, special shell characters,
and terminal input buffering as issues that can make your life difficult by munging your data or even hanging your
shell connection.
The Python Standard Library natively supports the Telnet protocol through its telnetlib module. Although
Telnet is ancient, is insecure, and can be difficult to script, it may often be the only protocol supported by simple
devices to which you want to connect.
The Secure Shell protocol is the current state of the art, not only for connecting to the command line of a remote
host but for copying files and forwarding TCP/IP ports as well. Python has quite excellent SSH support thanks to the
third-party paramiko package. When making an SSH connection, you need to remember three things.
• Paramiko will need to verify (or be told explicitly to ignore) the identity of the remote machine,
which is defined as the host key that it present when the connection is made.
• Authentication will typically be accomplished through a password or through the use of a
public-private key pair whose public half you have put in your authorized_keys file on the
remote server.
• Once you are authenticated, you can start all sorts of SSH services—remote shells, individual
commands, and file-transfer sessions—and they can all run at once without your having to
open new SSH connections, thanks to the fact that they will all get their own “channel” within
the master SSH connection.
The next chapter will examine an older and less capable protocol for file transfer that dates back to the early days
of the Internet: the File Transfer Protocol on which SFTP was based.