+ Reply to Thread
Results 1 to 3 of 3
  1. Senior Member Danielh22185's Avatar
    Join Date
    Apr 2012
    Location
    DFW Area
    Posts
    1,158

    Certifications
    CCNP R&S, CCNA, CCENT
    #1

    Default Python Script to Check Device for Output Change?

    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.Au toAddPolicy())
    ssh_client.connect(hostname=ip_address,username=us ername,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
    Reply With Quote Quote  

  2. SS -->
  3. Senior Member Danielh22185's Avatar
    Join Date
    Apr 2012
    Location
    DFW Area
    Posts
    1,158

    Certifications
    CCNP R&S, CCNA, CCENT
    #2
    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.Au toAddPolicy())
    ssh_client.connect(hostname=ip_address,username=us ername,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
    Last edited by Danielh22185; 07-26-2017 at 09:25 PM.
    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
    Reply With Quote Quote  

  4. Senior Member Danielh22185's Avatar
    Join Date
    Apr 2012
    Location
    DFW Area
    Posts
    1,158

    Certifications
    CCNP R&S, CCNA, CCENT
    #3
    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.Au toAddPolicy())
    ssh_client.connect(hostname=ip_address,username=us ername,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)
    Reply With Quote Quote  

+ Reply to Thread

Social Networking & Bookmarks