Paramiko (SSHv2) Python

Login to a switch with SSHv2 and run a command ‘show clock’:

Paramiko SSHv2 Login

>>> import paramiko
>>> import time
>>> ip_address = ‘192.168.1.1’
>>> username = ‘rwa’
>>> password = ‘rwa’
>>> ssh_client = paramiko.SSHClient()
>>> ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
>>> ssh_client.connect(hostname=ip_address,username=username,password=password)
>>> print “Successful connection”, ip_address
Successful connection 192.168.1.1
>>> remote_connection = ssh_client.invoke_shell()
>>> remote_connection.send(“show clock\n”)
11
>>> output = remote_connection.recv(65535)
>>> print output

>>> ssh_client.close()

 

EXTREME NETWORKS VOSS COMMAND LINE INTERFACE

core1:1>show clock

Fri Apr 20 11:24:39 2018 UTC

Note: This could be adapted to SSH to a remote switch and capture the running configuration and store in a filename by redirecting the output of the python script to a local file eg python showrun.py > showrun.cfg.

remote_connection.send(‘terminal more disable\n’)
remote_connection.send(‘enable\n’)
remote_connection.send(‘show run\n’)
remote_connection.send(‘terminal more enable\n’)

Found problems with capturing the output to a file so I changed the code and works better…

import paramiko
import time
import sys
ip_address = ‘192.168.1.1’
username = ‘rwa’
password = ‘rwa’
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(hostname=ip_address,username=username,password=password)
remote_connection = ssh_client.invoke_shell()
remote_connection.send(‘show clock\n’)
remote_connection.send(‘enable\n’)
remote_connection.send(‘terminal more disable\n’)
remote_connection.send(‘show run\n’)
remote_connection.send(‘terminal more enable\n’)

time.sleep(20)
readoutput = remote_connection.recv(655350)
saveoutput = open(‘Log file’, ‘w’)
print ‘Saving to file called “Log file”…’

saveoutput.write(readoutput)
saveoutput.write(‘\n’)
saveoutput.close

ssh_client.close()

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s