Before reading further, make yourself familiar with How to Install Python and SSH with Python
The code introduced in this article will basically do the following as if it were you:
login as: cisco
Using keyboard-interactive authentication.
Password:
R1>enable
Password:
R1#
Then execute a couple of commands automatically.
Script:
import paramiko
import cmd
import time
import sys
buff = ''
resp = ''
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('192.168.1.66', username='cisco', password='cisco')
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
chan = ssh.invoke_shell()
# first we enable!
chan.send('en\n')
time.sleep(1)
# enablepassword!
chan.send('cisco\n')
time.sleep(1)
# turn off paging
chan.send('terminal length 0\n')
time.sleep(1)
resp = chan.recv(9999)
#print (resp)
# Display how many users are connected to the IPSEC vpn
chan.send('sh ip int br')
chan.send('\n')
time.sleep(1)
resp = chan.recv(9999)
output = resp.decode('ascii').split(',')
print (''.join(output))
# Display how many users are connected to the IPSEC vpn
chan.send('sh ver')
chan.send('\n')
time.sleep(1)
resp = chan.recv(9999)
output = resp.decode('ascii').split(',')
print (''.join(output))
ssh.close()