Friday, August 5, 2016

Python notes as it pertains to networking!

Getting it setup on my Raspberry Pi
So python comes pre-installed on the raspberry pi!  Neat..but after following some guides here...

https://pynet.twb-tech.com/blog/automation/netmiko.html

I've decided to give this a try to see if I can use Python to SSH into my existing devices.  This will help facilitate anything I'll need to do down the road!  Some things I've read is that one of the benefits Python has over Poweshell is that Python supports SSH!

So one thing that I've learned (in my ignorant Python experience) is that you can use the Paramiko library to SSH into "stuff."  I tried messing around with it and was able to successfully SSH into my home 2811...but the syntax was...not very user-friendly (for someone who is as new to Python as I am).

I wanted to get into use "Netmiko" see link above..but found that it wouldn't work!  Now, I'll go over some things I tried..

1. I downloaded the setup.py file and manually installed it "python setup.py install --user."
2. Once it successfully installed, I tried launching python via "python" to see if it would import the ConnectHandler function from the Netmiko module.
3. No bueno!  Every time I'd get to calling the ConnectHandler...I'd get the following error:


No handlers could be found for logger "paramiko.transport" Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/home/admin/.local/lib/python2.7/site-packages/netmiko-0.5.6-py2.7.egg/netmiko/ssh_dispatcher.py", line 84, in ConnectHandler return ConnectionClass(*args, **kwargs) File "/home/admin/.local/lib/python2.7/site-packages/netmiko-0.5.6-py2.7.egg/netmiko/base_connection.py", line 68, in __init__ self.establish_connection(verbose=verbose, use_keys=use_keys, key_file=key_file) File "/home/admin/.local/lib/python2.7/site-packages/netmiko-0.5.6-py2.7.egg/netmiko/base_connection.py", line 177, in establish_connection self.remote_conn_pre.connect(**ssh_connect_params) File "/home/admin/.local/lib/python2.7/site-packages/paramiko-2.0.2-py2.7.egg/paramiko/client.py", line 338, in connect t.start_client() File "/home/admin/.local/lib/python2.7/site-packages/paramiko-2.0.2-py2.7.egg/paramiko/transport.py", line 493, in start_client raise e AttributeError: 'EntryPoint' object has no attribute 'resolve'

Now, a little googling presented the following:

https://github.com/mozilla/sops/issues/67

Should the link die..

"Yep, downgrading to cryptography 1.2.1 fixed it. Thank you!"

Also, after discussing my conondrum in a reddit post, someone else recommended installing PIP, a Python package manager.

sudo apt-get install python-pip

Hindsight is 20/20..but I could have simply used PIP to install netmiko via "pip install netmiko."

So finally, with PIP installed..

sudo pip install cryptography==1.2.1

Huzzah, it works!

>>> from netmiko import ConnectHandler
>>> my_router = {
...     'device_type': 'cisco_ios',
...     'ip': '10.0.0.1',
...     'username': 'admin',
...     'password': '<password here>',
...     }
>>> net_connect = ConnectHandler(**my_router)
>>> output = net_connect.send_command("show ip int brief")
>>> print output
Interface                  IP-Address      OK? Method Status                Protocol
FastEthernet0/0            <My public>    YES DHCP   up                    up
FastEthernet0/1            unassigned      YES NVRAM  administratively down down
FastEthernet1/0            unassigned      YES unset  up                    up
FastEthernet1/1            unassigned      YES unset  up                    up
FastEthernet1/2            unassigned      YES unset  up                    down