<jupyter_start><jupyter_text>Engineering Summit 2019Denver, ColoradoAuthor: <NAME> <EMAIL>  Connecting to DevicesIn this notebook we'll illustrate connecting to a device via SSH. The target device in this case is my own laptop for simplicity.Do take this and adapt it other use cases. You will see how challenging it can be dealing with network delay, buffers, `stdin`/`stdout`/`stderr`, and weird device-specific behavior that needs workarounds. Suffice to say that automating infrastructure in this way is extraordinarily cumbersome. That's what REST and XML APIs are for. However, connecting to a device's shell via SSH can be handy to do quick checks or perform basic templated setup.  ParamikoFor this exercise we'll be using [Paramiko](http://www.paramiko.org/installing.html).You'll need to run the following command in a console to download this library.* Anaconda users:    `conda install -c conda-forge paramiko`* Non-Anaconda users (python):    `pip install paramiko`Read more docs here: http://docs.paramiko.org/en/stable/index.htmlYou will also need to enable a local SSH listener if you want to follow along.Mac users: `sudo systemsetup -setremotelogin on`**NOTE** Much of Python programing is finding the right library to use. If you think you have to write it from scratch, you are probably doing it wrong. Python has a **huge** set of libraries available that make programming with it very easy. There are a ton of online resources that help you navigate that. $$\large-\infty-$$<jupyter_code># environment setup
import getpass
import paramiko
import time<jupyter_output><empty_output><jupyter_text>$$\large-\infty-$$<jupyter_code>host = '127.0.0.1'
username = 'mono'
port = 22
password = getpass.getpass('password for ' + username + ' : ')
prompt = '$'

# set up the connection
client = paramiko.SSHClient()

# deal with ssh keys
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.load_system_host_keys()

try:
    # connect to host
    client.connect(host, port, username, password)

    # create shell
    shell = client.invoke_shell()
    print('Connected to ' + host)
except:
    print('Something went wrong. :-(')
    client.close()
    
stdin, stdout, stderr = client.exec_command('which python')

for line in stdout:
    print(line)
    
stdin, stdout, stderr = client.exec_command('ls')

for line in stdout:
    print(line)

client.close()<jupyter_output>password for mono : ········
Connected to 127.0.0.1
/usr/bin/python

Applications

Desktop

Documents

Downloads

Dropbox

Library

Movies

Music

Pictures

Public

anaconda3
{'fraction_non_alphanumeric': 0.08864696734059098, 'fraction_numerical': 0.007387247278382581, 'mean_word_length': 4.5813449023861175, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '442557', 'license': 'MIT', 'n_tokens_mistral': 791, 'n_tokens_neox': 742, 'n_words': 300, 'path': 'Summit19_Python_03_Devices.ipynb', 'repo_name': 'victoraranda/summit19', 'size': '2596'}