Hello, i have written the below code to go out and look on a Cisco device, return output, write to a file and then save the file and close it off

Okay Massive edit to better ask what im seeking:

I have the below code, which thanks to the answer below now 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()


ANSWERS:


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()


 MORE:


 ? How does the switch react when the mac address table is cleared?
 ? Apache Velocity: Checking the count of array
 ? Tcl [regexp]: Expression to match if block with variable inside
 ? Tcl lappend returning unexpected results
 ? invoke a tcl script from another tcl script with multiple arguements
 ? In a tcl script how can i use puts to write a string to the console and to a file at the same time?
 ? Regex pattern behaving differently in TCL compared with Perl & Python
 ? How to bypass window shell (wish/tclsh) GUI and use terminal instead?
 ? How to read the variable Name which store the value
 ? Different version of tcl giving different answers