Evil_TTL> show | s

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

# first we enable!
chan.send('en\n')
time.sleep(1)
#resp = chan.recv(9999)
#print resp

# enablepassword!
chan.send('cisco\n')
time.sleep(1)
#resp = chan.recv(9999)
#print resp

# get into config mode
#chan.send('conf t\n')
#time.sleep(1)
#resp = chan.recv(9999)
#print resp

# 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('\n')
for 
line in output:
    if 
'up' in line:
        print (
line)

ssh.close() 

Output with condition as per above code:

FastEthernet0/0            192.168.1.66    YES NVRAM  up                    up 

Output without condition:

sh ip int br
Interface                  IP-Address      OKMethod Status                Protocol
FastEthernet0
/0            192.168.1.66    YES NVRAM  up                    up      
FastEthernet0
/1            unassigned      YES NVRAM  administratively down down    
R1
By privilege15