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 />

» » return<br />

client = paramiko.SSHClient()<br />

client.set_missing_host_key_policy(AllowAnythingPolicy())<br />

client.connect('127.0.0.1', username='test') # password='')<br />

def read_until_EOF(fileobj):<br />

» s = fileobj.readline()<br />

» while s:<br />

» » print s.strip()<br />

» » s = fileobj.readline()<br />

out1 = client.exec_command('echo One;sleep 2;echo Two;sleep 1;echo Three')[1]<br />

out2 = client.exec_command('echo A;sleep 1;echo B;sleep 2;echo C')[1]<br />

thread1 = threading.Thread(target=read_until_EOF, args=(out1,))<br />

thread2 = threading.Thread(target=read_until_EOF, args=(out2,))<br />

thread1.start()<br />

thread2.start()<br />

thread1.join()<br />

thread2.join()<br />

client.close()<br />

In order to be able to process these two streams <strong>of</strong> data simultaneously, we are kicking <strong>of</strong>f two<br />

threads, and are handing each <strong>of</strong> them one <strong>of</strong> the channels from which to read. They each print out each<br />

line <strong>of</strong> new information as soon as it arrives, and finally exit when the readline() command indicates<br />

end-<strong>of</strong>-file by returning an empty string. When run, this script should return something like this:<br />

$ python ssh_threads.py<br />

One<br />

A<br />

B<br />

Two<br />

Three<br />

C<br />

So there you have it: SSH channels over the same TCP connection are completely independent, can<br />

each receive (and send) data at their own pace, and can close independently when the particular<br />

command that they are talking to finally terminates.<br />

The same is true <strong>of</strong> the features we are about to look at—file transfer and port forwarding—so keep<br />

in mind as you read our last two examples that all <strong>of</strong> these kinds <strong>of</strong> communications can happen<br />

simultaneously without your having to open more than one SSH connection to hold all <strong>of</strong> the channels<br />

<strong>of</strong> data.<br />

SFTP: File Transfer Over SSH<br />

Version 2 <strong>of</strong> the SSH protocol includes a sub-protocol called the “SSH File Transfer Protocol” (SFTP) that<br />

lets you walk the remote directory tree, create and delete directories and files, and copy files back and<br />

forth from the local to the remote machine. The capabilities <strong>of</strong> SFTP are so complex and complete, in<br />

fact, that they support not only simple file-copy operations, but can power graphical file browsers and<br />

can even let the remote filesystem be mounted locally! (Google for the sshfs system for details.)<br />

The SFTP protocol is an incredible boon to those <strong>of</strong> us who once had to copy files using brittle<br />

scripts that tried to send data across Telnet through very careful escaping <strong>of</strong> binary data! And instead <strong>of</strong><br />

286

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

Saved successfully!

Ooh no, something went wrong!