Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Python 3:
- sudo apt-get install build-essential libssl-dev libffi-dev python3-dev
- sudo pip3 install cryptography
- sudo pip3 install paramiko
- $ python3
- Python 3.5.2 (default, Nov 17 2016, 17:05:23)
- [GCC 5.4.0 20160609] on linux
- Type "help", "copyright", "credits" or "license" for more information.
- >>> import paramiko
- >>>
- The Paramiko overview
- Let's look at a quick example using the Python 3 interactive shell:
- >>> import paramiko, time
- >>> connection = paramiko.SSHClient()
- >>> connection.set_missing_host_key_policy(paramiko.AutoAddPolicy())
- >>> connection.connect('172.16.1.20', username='cisco', password='cisco',
- look_for_keys=False, allow_agent=False)
- >>> new_connection = connection.invoke_shell()
- >>> output = new_connection.recv(5000)
- >>> print(output)
- b"rn***********************************************************************
- ***rn* IOSv is strictly limited to use for evaluation, demonstration and
- IOS *rn* education. IOSv is provided as-is and is not supported by Cisco's
- *rn* Technical Advisory Center. Any use or disclosure, in whole or in part,
- *rn* of the IOSv Software or Documentation to any third party for any *rn*
- purposes is expressly prohibited except as otherwise authorized by *rn*
- Low-Level Network Device Interactions
- [ 59 ]
- Cisco in writing.
- *rn************************************************************************
- **rniosv-1#"
- >>> new_connection.send("show version | i Vn")
- 19
- >>> time.sleep(3)
- >>> output = new_connection.recv(5000)
- >>> print(output)
- b'show version | i VrnCisco IOS Software, IOSv Software (VIOSADVENTERPRISEK9-M),
- Version 15.6(2)T, RELEASE SOFTWARE (fc2)rnProcessor
- board ID 9MM4BI7B0DSWK40KV1IIRrniosv-1#'
- >>> new_connection.close()
- >>>
- The time.sleep() function inserts time delay to ensure that all the
- outputs were captured. This is particularly useful on a slower network
- connection or a busy device. This command is not required but
- recommended depending on your situation.
- Even if you are seeing the Paramiko operation for the first time, the beauty of Python and
- its clear syntax means that you can make a pretty good educated guess at what the program
- is trying to do:
- >>> import paramiko
- >>> connection = paramiko.SSHClient()
- >>> connection.set_missing_host_key_policy(paramiko.AutoAddPolicy())
- >>> connection.connect('172.16.1.20', username='cisco', password='cisco',
- look_for_keys=False, allow_agent=False)
- The first four lines create an instance of the SSHClient class from Paramiko. The next line
- sets the policy that the client should use when the SSH server's hostname, in this case
- iosv-1, is not present in either the system host keys or the application's keys. In this case,
- we will just automatically add the key to the application's HostKeys object. At this point, if
- you log onto the router, you will see the additional login session from Paramiko:
- iosv-1#who
- Line User Host(s) Idle Location
- *578 vty 0 cisco idle 00:00:00 172.16.1.1
- 579 vty 1 cisco idle 00:01:30 172.16.1.173
- Interface User Mode Idle Peer Address
- iosv-1#
- The next few lines invokes a new interactive shell from the connection and a repeatable
- pattern of sending a command and retrieves the output. Finally we close the connection.
Add Comment
Please, Sign In to add comment