Evil_TTL> show | s

Execute MULTIPLE commands with Python

Category:Automation -> Python

Before reading further, make yourself familiar with How to Install Python and SSH with Python

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()

# turn off paging
chan.send('terminal length 0\n')
time.sleep(1)
resp chan.recv(9999)
output resp.decode('ascii').split(',')
#print (''.join(output))

# Display output of first command
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 output of second command
chan.send('sh ver')
chan.send('\n')
time.sleep(1)
resp chan.recv(9999)
output resp.decode('ascii').split(',')
print (
''.join(output))

ssh.close() 

In python v3 one needs to decode output by output = resp.decode(‘ascii’).split(’,’), else the following error will appear:

TypeErrorsequence item 0expected str instanceint found 
By privilege15