Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Netmiko Loop question
#1
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)

================================================
Reply
#2
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
Larz60+ write Oct-24-2020, 10:29 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.

Added for you this time
Reply
#3
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)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  for loop question KEYS 1 428 Oct-27-2020, 11:42 PM
Last Post: jefsummers
  while loop question KEYS 2 495 Sep-26-2020, 11:02 PM
Last Post: KEYS
  New to programming, loop question tomyan 1 420 Sep-25-2020, 04:32 PM
Last Post: Larz60+
  while loop question spalisetty06 2 564 Aug-13-2020, 04:18 PM
Last Post: buran
  question about for loop Than999 5 876 Jun-09-2020, 02:16 PM
Last Post: Emekadavid
  Sending Advanced Commands with Netmiko rogueakula 1 829 Oct-22-2019, 07:54 PM
Last Post: rogueakula
  Question about for loop not creating an infinite loop. FWendeburg 1 1,171 Feb-03-2019, 08:45 PM
Last Post: ichabod801
  Loop Condition Question malonn 6 1,592 Aug-01-2018, 01:56 PM
Last Post: malonn
  Beginner Loop question BigDisAok 5 1,817 Jul-24-2018, 02:04 PM
Last Post: BigDisAok
  Scoping Question: If else in for loop not evaluating i as expected datasundae 3 1,610 May-25-2018, 06:43 PM
Last Post: datasundae

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020