Python Script to Check Device for Output Change?

Danielh22185Danielh22185 Member Posts: 1,195 ■■■■□□□□□□
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
Currently Studying: IE Stuff...kinda...for now...
My ultimate career goal: To climb to the top of the computer network industry food chain.
"Winning means you're willing to go longer, work harder, and give more than anyone else." - Vince Lombardi

Comments

  • Danielh22185Danielh22185 Member Posts: 1,195 ■■■■□□□□□□
    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
    Currently Studying: IE Stuff...kinda...for now...
    My ultimate career goal: To climb to the top of the computer network industry food chain.
    "Winning means you're willing to go longer, work harder, and give more than anyone else." - Vince Lombardi
  • Danielh22185Danielh22185 Member Posts: 1,195 ■■■■□□□□□□
    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)
    Currently Studying: IE Stuff...kinda...for now...
    My ultimate career goal: To climb to the top of the computer network industry food chain.
    "Winning means you're willing to go longer, work harder, and give more than anyone else." - Vince Lombardi
Sign In or Register to comment.