Python Script to Execute Show Commands in Multiple Devices and Save the Output to a Text File

The script requires two text files, put your device IP addresses in IPAddressList.txt and create another blank file named Command_Output.txt in the application directory .

Prerequisites

from future import print_function
from netmiko import ConnectHandler
import os
import sys
import time
import select
import paramiko
import re

fd = open(‘r’\home\user\Command_Output.txt’,’w’)
old_stdout = sys.stdout
sys.stdout = fd
platform = ‘cisco_ios’
username = ‘XXXX’
password = ‘XXXX’
ip_add_file = open(r’\home\user\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(‘sh int trunk’)
print(output)

fd.close()

renjithbs Avatar

Posted by

Leave a comment