I have the below code, which will go and look in the .txt file for the IP address and then go to the device and return the commands, it will then print to the file requested and everything works.
What i cant get it to do is loop through a series of IP addresses and return the commands for different 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 due to when only a single address is in the file it works seemlessly.
I am seeking a way to loop through 10 IP addresses and run the same commands when all is said and done.
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()
For clarity the answer given below is perfect for what i have asked so the start of the code will look like this:
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:
host = host.strip()
device = ConnectHandler(device_type=platform, ip=host, username=username, password=password)
output = device.send_command('terminal length 0')
And so on for what ever commands need to be run.
My guess is that your ConnectHandler
does not appreciate that there is a newline character at the end of your host
string.
Try this:
for host in ip_add_file:
host = host.strip()
...