Search code examples
pythonpython-3.xciscocisco-ios

How to loop over list of IP addresses, one per line in Python


I have the below code which will open the .txt file containing IP addresses and then connect to the device and capturethe command output, it will then print the output to file and state that everything works.

I can't get it to loop through a series of IP addresses and return the command output for multiple devices. I get an error of the script timing out when I add in more than one IP to the .txt list. This is proven by adding the same address twice so i know the addresses are good, compared to when only a single address is in the file and it works seemlessly.

I am seeking a way to loop through 10 IP addresses and run the same commands:

from __future__ import print_function
from netmiko import ConnectHandler

import sys
import time
import select
import paramiko
import re
fd = open(r'C:\Users\NewdayTest.txt','w') 
old_stdout = sys.stdout   
sys.stdout = fd 
platform = 'cisco_ios'
username = 'Username'
password = 'Password'

ip_add_file = open(r'C:\Users\\IPAddressList.txt','r') 

for host in ip_add_file:
    device = ConnectHandler(device_type=platform, ip=host, username=username, password=password)
    output = device.send_command('terminal length 0')
    output = device.send_command('enable')
    print('##############################################################\n')
    print('...................CISCO COMMAND SHOW RUN OUTPUT......................\n')
    output = device.send_command('sh run')
    print(output)
    print('##############################################################\n')
    print('...................CISCO COMMAND SHOW IP INT BR OUTPUT......................\n')
    output = device.send_command('sh ip int br')
    print(output) 
    print('##############################################################\n')

fd.close()

Solution

  • Keep in mind that every line will be a new IP addresses.

    And you're not writing to ciscoOutput file , you can use command fd.write('text') for that.

    from __future__ import print_function
    from netmiko import ConnectHandler
    
    import sys
    import time
    import select
    import paramiko
    import re
    fd = open(r'C:\Users\LocationOfMyFile\CiscoOutput.txt','w') 
    old_stdout = sys.stdout   
    sys.stdout = fd 
    platform = 'cisco_ios'
    username = 'My Username'
    password = 'My Password'
    
    ip_add_file = open('file_name.txt','r') 
    
    for host in ip_add_file:
    
        device = ConnectHandler(device_type=platform, ip=host, username=username, password=password)
    
    
        output = device.send_command('show version')
        print(output)
    
    
        output = device.send_command('terminal length 0')
        print(output)
    
    
        output = device.send_command('sh ip int br')
        print(output)
    
    
        output = device.send_command('show interfaces GigabitEthernet0/1')
        print(output)
    
    fd.close()