Categories
Welcome Center
Education & Development
Discussions
Certification Preparation
Recent Posts
Groups
Free Resources
Ebooks
Free Workshops
Trending Certifications Infographic
Infosec Training
IT & Security Training
Live Boot Camps
Security Awareness Training
About Infosec Institute
Home
Certification Preparation
Cisco
CCNP (Professional)
Python Script to Check Device for Output Change?
Danielh22185
Not sure if this is the best place to post something like this but I have an issue I am working with Cisco TAC on to troubleshoot some SNMP issues affecting one of my 3850 switches.
They want me to run this command until several "RAs" are seen in the output:
'show stacks 284'
I am pretty new to Python so I am just getting my feet wet trying to utilize it to automate tasks for network support. I was able to create a script that logs into the switch and gathers this output, however I want it to repeat entering that command unending. Preferably under the same SSH connection (so I don't blow up my VTY lines). I also don't want to blow up my switch CPU either with hammering it with that command insanely fast infinitely many times.
Any python experts in here that can help with this?
My sample code so far:
import paramiko
import time
ip_address = "192.168.1.1"
username = "username"
password = "password"
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
remote_connection = ssh_client.invoke_shell()
remote_connection.send("show clock\n")
remote_connection.send("show stacks 284\n")
time.sleep(1)
output = remote_connection.recv(65535)
print output
ssh_client.close
Find more posts tagged with
Save $250 on 2025 certification boot camps from Infosec!
Book now with code EOY2025
Button
Comments
Danielh22185
Well that was easier than I thought. I atleast figured out a way to get the commands looping via the iteration:
Now I just need to figure out a way how to build this info a file.
import paramiko
import time
ip_address = "192.168.1.1"
username = "username"
password = "password"
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
remote_connection = ssh_client.invoke_shell()
while True:
remote_connection.send("show clock\n")
remote_connection.send("show stacks 284\n")
time.sleep(1)
output = remote_connection.recv(65535)
print output
Danielh22185
With some help from RouterGods I was able to craft the final iteration. Feel free to use this for anything you would like to run a repetitious command and collect the output into a building file structure.
import paramiko
import time
ip_address = "192.168.1.1"
username = "username"
password = "password"
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
remote_connection = ssh_client.invoke_shell()
while True:
remote_connection.send("show clock\n")
remote_connection.send("show stacks 284\n")
time.sleep(1)
output = remote_connection.recv(65535)
with open("output.log", "a") as output_log:
output_log.write(output)
Quick Links
All Categories
Recent Posts
Activity
Unanswered
Groups
Best Of
INFOSEC Boot Camps
$250
OFF
Use code
EOY2025
to receive $250 off your 2025 certification boot camp!
BROWSE BOOT CAMPS