09.11.2016 Views

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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CHAPTER 16 ■ TELNET AND SSH<br />

replaced by more text. Often these were the characters Ctrl+S for “Stop” and Ctrl+Q for “Keep going,”<br />

and it was a source <strong>of</strong> great annoyance that if binary data worked its way into an automated Telnet<br />

connection that the first Ctrl+S that happened to pass across the channel would pause the terminal and<br />

probably ruin the session.<br />

Again, this setting can be turned <strong>of</strong>f with stty:<br />

$ stty -ixon -ix<strong>of</strong>f<br />

Those are the two biggest problems you will run into with terminals doing buffering, but there are<br />

plenty <strong>of</strong> less famous settings that can also cause you grief. Because there are so many—and because<br />

they vary between Unix implementations—the stty command actually supports two modes, cooked and<br />

raw, that turn dozens <strong>of</strong> settings like icanon and ixon on and <strong>of</strong>f together:<br />

$ stty raw<br />

$ stty cooked<br />

In case you make your terminal settings a hopeless mess after some experimentation, most Unix<br />

systems provide a command for resetting the terminal back to reasonable, sane settings (and note that if<br />

you have played with stty too severely, you might need to hit Ctrl+J to submit the reset command, since<br />

your Return key, whose equivalent is Ctrl+M, actually only functions to submit commands because <strong>of</strong> a<br />

terminal setting called icrnl!):<br />

$ reset<br />

If, instead <strong>of</strong> trying to get the terminal to behave across a Telnet or SSH session, you happen to be<br />

talking to a terminal from <strong>Python</strong>, check out the termios module that comes with the Standard Library.<br />

By puzzling through its example code and remembering how Boolean bitwise math works, you should<br />

be able to control all <strong>of</strong> the same settings that we just accessed through the stty command.<br />

This book lacks the space to look at terminals in any more detail (since one or two chapters <strong>of</strong><br />

examples could easily be inserted right here to cover all <strong>of</strong> the interesting techniques and cases), but<br />

there are lots <strong>of</strong> great resources for learning more about them—a classic is Chapter 19, “Pseudo<br />

Terminals,” <strong>of</strong> W. Richard Stevens' Advanced <strong>Programming</strong> in the UNIX Environment.<br />

Telnet<br />

This brief section is all you will find in this book about the ancient Telnet protocol. Why? Because it is<br />

insecure: anyone watching your Telnet packets fly by will see your username, password, and everything<br />

you do on the remote system. It is clunky. And it has been completely abandoned for most systems<br />

administration.<br />

274

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

Saved successfully!

Ooh no, something went wrong!