09.11.2016 Views

Foundations of Python Network Programming 978-1-4302-3004-5

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

CHAPTER 16 ■ TELNET AND SSH<br />

particular, unless you have spent hours trying to transfer a file between computers armed with only<br />

Telnet and a script that tries to type your password for you, only to discover that your file contains a byte<br />

that looks like a control character to Telnet or the remote terminal, and have the whole thing hang until<br />

you add a layer <strong>of</strong> escaping (or figure out how to disable both the Telnet escape key and all<br />

interpretation taking place on the remote terminal).<br />

But the best feature <strong>of</strong> the rlogin family was that they did not just echo username and password<br />

prompts without actually knowing the meaning <strong>of</strong> what was going on. Instead, they stayed involved<br />

through the process <strong>of</strong> authentication, and you could even create a file in your home directory that told<br />

them “when someone named brandon tries to connect from the asaph machine, just let them in without a<br />

password.” Suddenly, system administrators and Unix users alike received back hours <strong>of</strong> each month<br />

that would otherwise have been spent typing their password. Suddenly, you could copy ten files from<br />

one machine to another nearly as easily as you could have copied them into a local folder.<br />

SSH has preserved all <strong>of</strong> these great features <strong>of</strong> the early remote-shell protocol, while bringing<br />

bulletpro<strong>of</strong> security and hard encryption that is trusted worldwide for administering critical servers. This<br />

chapter will focus on SSH-2, the most recent version <strong>of</strong> the protocol, and on the paramiko <strong>Python</strong><br />

package that can speak the protocol—and does it so successfully that it has actually been ported to Java,<br />

too, because people in the Java world wanted to be able to use SSH as easily as we do when using<br />

<strong>Python</strong>.<br />

An Overview <strong>of</strong> SSH<br />

You have reached a point in this book where something very interesting happens: we encounter a new<br />

layer <strong>of</strong> multiplexing.<br />

The first section <strong>of</strong> this book talked a lot about multiplexing—about how UDP (Chapter 2) and TCP<br />

(Chapter 3) take the underlying IP protocol, which has no concept that there might actually be several<br />

users or applications on a single computer that need to communicate, and add the concept <strong>of</strong> UDP and<br />

TCP port numbers, so that several different conversations between a pair <strong>of</strong> IP addresses can take place<br />

at the same time.<br />

Once that basic level <strong>of</strong> multiplexing was established, we more or less left the topic behind. Through<br />

more than a dozen chapters now, we have studied protocols that take a UDP or TCP connection and<br />

then happily use it for exactly one thing—downloading a web page, or transmitting an e-mail, but never<br />

trying to do several things at the same time over a single socket.<br />

But as we now arrive at SSH, we reach a protocol so sophisticated that it actually implements its<br />

own rules for multiplexing, so that several “channels” <strong>of</strong> information can all share the same SSH socket.<br />

Every block <strong>of</strong> information SSH sends across its socket is labeled with a “channel” identifier so that<br />

several conversations can share the socket.<br />

There are at least two reasons sub-channels make sense. First, even though the channel ID takes up<br />

a bit <strong>of</strong> bandwidth for every single block <strong>of</strong> information transmitted, the additional data is small<br />

compared to how much extra information SSH has to transmit to negotiate and maintain encryption<br />

anyway. Second, channels make sense because the real expense <strong>of</strong> an SSH connection is setting it up.<br />

Host key negotiation and authentication can together take up several seconds <strong>of</strong> real time, and once the<br />

connection is established, you want to be able to use it for as many operations as possible. Thanks to the<br />

SSH notion <strong>of</strong> a channel, you can amortize the high cost <strong>of</strong> connecting by performing many operations<br />

before you let the connection close.<br />

Once connected, you can create several kinds <strong>of</strong> channels:<br />

• An interactive shell session, like that supported by Telnet<br />

• The individual execution <strong>of</strong> a single command<br />

• A file-transfer session letting you browse the remote filesystem<br />

• A port-forward that intercepts TCP connections<br />

279

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!