Chapter 16 ■ telnet and SSh
303
if name == 'main':
parser = argparse.ArgumentParser(description='Use Telnet to log in')
parser.add_argument('hostname', help='Remote host to telnet to')
parser.add_argument('username', help='Remote username')
args = parser.parse_args()
password = getpass.getpass('Password: ')
main(args.hostname, args.username, password)
For more details about how Telnet options work, again you can consult the relevant RFCs. In the next section,
I will leave behind the ancient insecure Telnet protocol and begin discussing a modern and safe approach to running
remote commands.
SSH: The Secure Shell
The SSH protocol is one of the best-known examples of a secure, encrypted protocol (HTTPS is probably
the best known).
■ THE SSH PROTOCOL
purpose: Secure remote shell, file transfer, port forwarding
Standard: rFC 4250–4256 (2006)
runs atop: tCp/Ip
default port: 22
library: paramiko
exceptions: socket.error, socket.gaierror, paramiko.SShexception
SSH is descended from an earlier protocol that supported “remote login,” “remote shell,” and “remote file copy”
commands named rlogin, rsh, and rcp, which in their time tended to become much more popular than Telnet at
sites that supported them. You cannot imagine what a revelation rcp was, in particular, unless you have spent hours
trying to transfer a binary file between computers armed with only Telnet and a script that tries to type your password
for you, only to discover that your file contains a byte that looks like a control character to Telnet or the remote
terminal, causing the whole thing to hang until you add a layer of escaping (or figure out how to disable both the
Telnet escape key and all interpretation taking place on the remote terminal).
However, the best feature of the rlogin family members was that they did not just echo username and password
prompts without actually knowing the meaning of what was going on. Instead, they stayed involved throughout the
process of authentication, and you could even create a file in your home directory that told them “When someone
named brandon tries to connect from the asaph machine, just let them in without a password.” Suddenly, system
administrators and Unix users alike received back hours each month that would have otherwise been spent typing
their password. Moreover, suddenly you could rcp copy ten files from one machine to another nearly as easily as you
could have copied them into a local folder.
SSH has preserved all of these great features of the early remote-shell protocol while bringing about security and
hard encryption that is trusted worldwide for administering critical servers. This chapter will focus on the third-party
paramiko Python package that can speak the SSH protocol and does it so successfully that it has actually been ported
to Java as well because people in the Java world wanted to be able to use SSH as easily as we do when using Python.