Python Forum
Netmiko Loop question - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Netmiko Loop question (/thread-30531.html)



Netmiko Loop question - sc00ter - Oct-24-2020

I'm trying to figure out how to loop over a list of interface and run a TDR (Time Division Reflectometer) on Cisco switch ports.

I'm able to get the list of interfaces I want to test on the switch.

I want to create a variable and split [0] to get the interfaces.

Then use the variable list to run the test command, and then the show command.

1. test cable-diagnostics tdr int $var
2. pause 5 seconds
3. show cable-diagnostics tdr int $var

Any help would be greatly appreciated.

Thank you for your time!


----------

I can get to this point and get this output. I'm using netmiko module, and running on a cisco 4500 in a lab.

GigabitEthernet1/1 unassigned YES unset up up
GigabitEthernet1/11 unassigned YES unset up up
GigabitEthernet1/14 unassigned YES unset up up
GigabitEthernet1/16 unassigned YES unset up up
GigabitEthernet1/17 unassigned YES unset up up
GigabitEthernet1/19 unassigned YES unset up up

================================================

var = o1.split0

GigabitEthernet1/1
GigabitEthernet1/11
GigabitEthernet1/14
GigabitEthernet1/16
GigabitEthernet1/17
GigabitEthernet1/19

================================================

# TEST cable-diagnostics tdr interface commands

testdr = [

# test cable-diagnostics tdr interface + $var


# SHOW cable-diagnostics tdr interface commands

shtdr = [

# show cable-diagnostics tdr interface + $var

print (shtdr)

================================================


RE: Netmiko Loop question - sc00ter - Oct-24-2020

Here's my script so far. I apologize if sloppy and crude. New to netmiko, and trying to test network cables and make my life easier.


#!/usr/bin/env python
from netmiko import Netmiko
from netmiko import ConnectHandler
import time
import re

cisco = {
     'device_type': 'cisco_ios',
     'host': '10.1.1.1',
     'username': 'cisco',
     'password': 'cisco',
     'timeout': 35 * 60,
}


print()
print("running commands...")
net_connect = ConnectHandler(**cisco)


output = (net_connect.send_command('sh run | i hostname ').split()[1])
 
print()
print()   
print ('HOSTNAME:')
print()   
print(output)

output = (net_connect.send_command('sh run | i ip address 10 ').split()[2])  
print(output)

print()   
o1 = net_connect.send_command('show ip int brief | include Ethernet1/1.*up')
print(o1)
Output:
HOSTNAME: 4500lab 10.1.1.1 GigabitEthernet1/1 unassigned YES unset up up GigabitEthernet1/11 unassigned YES unset up up GigabitEthernet1/14 unassigned YES unset up up GigabitEthernet1/16 unassigned YES unset up up GigabitEthernet1/17 unassigned YES unset up up GigabitEthernet1/19 unassigned YES unset up up



RE: Netmiko Loop question - sc00ter - Oct-24-2020

I was able to get this to work. Prompts for IP Address and switchport number. Not as elegant as automatically running for all "up" interfaces, but it will have to do for now. I'll keep playing and learning with loops.

I added the global delay of 7 seconds to wait between the running of the "test cable tdr int x" and the "show cable tdr int x".

Maybe this will help someone down the road.

Well, back to reading more on python loops and nested loops.

Cheers!



#!/usr/bin/env python

from netmiko import Netmiko
from netmiko import ConnectHandler
import time
import re

ipaddr = input ('Enter the IP: ')
interface = input ('Enter the switchport number to be tested: ')

cisco = {
'device_type': 'cisco_ios',
'host': ipaddr,
'username': 'cisco',
'password': 'cisco',
'timeout': 35 * 60,
'global_delay_factor': 7,
}


net_connect = ConnectHandler(**cisco)

output1 = net_connect.send_command('test cable tdr int ' + interface)

output2 = net_connect.send_command('sh cable tdr int ' + interface)
print()
print('TDR TEST RESULTS FOR INTERFACE: ')
print()
print(output2)