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
  I have an issue with Netmiko Error reading SSH protocol banner omarhegazy 2 3,513 May-16-2022, 06:05 PM
Last Post: omarhegazy
  A question about 'Event loop is closed' fc5igm 2 2,165 Oct-05-2021, 02:00 AM
Last Post: fc5igm
Exclamation question about input, while loop, then print jamie_01 5 2,616 Sep-30-2021, 12:46 PM
Last Post: Underscore
  for loop question KEYS 1 1,697 Oct-27-2020, 11:42 PM
Last Post: jefsummers
  while loop question KEYS 2 1,976 Sep-26-2020, 11:02 PM
Last Post: KEYS
  New to programming, loop question tomyan 1 1,589 Sep-25-2020, 04:32 PM
Last Post: Larz60+
  while loop question spalisetty06 2 1,813 Aug-13-2020, 04:18 PM
Last Post: buran
  question about for loop Than999 5 2,427 Jun-09-2020, 02:16 PM
Last Post: Emekadavid
  Sending Advanced Commands with Netmiko rogueakula 1 1,971 Oct-22-2019, 07:54 PM
Last Post: rogueakula
  Question about for loop not creating an infinite loop. FWendeburg 1 2,080 Feb-03-2019, 08:45 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

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