Python Forum

Full Version: Assistance with Python Network script
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello all! I need to push out a new configuration to about 2000+ Cisco IOS devices and about 1000+ Fortinet firewall appliances. I found a nice script that does most of what I want but won't execute configuration commands. For some reason if I alter the configuration it won't accept the configuration commands. Since I have a huge IP listing I will need to call the IP addresses from a file and to be able to log the results of the output to a file.

Below is the script I found on the internet that works great for issuing show commands:

from __future__ import print_function
from netmiko import ConnectHandler
import sys
import time
import select
import paramiko
import re
fd = open(r'C:\Command_Output.txt','w') # Where you want the file to save to.
old_stdout = sys.stdout   
sys.stdout = fd 
platform = 'cisco_ios'
username = 'admin' # edit to reflect
password = 'XXXXX' # edit to reflect
 
ip_add_file = open(r'C:\IPAddressList.txt','r') # a simple list of IP addresses you want to connect to each one on a new line
 
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')
    # output = device.send_command('enable') #Editable to be what ever is needed - Uncomment by removing the # at the begining of the line
    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()
If I try to alter the commands such as output = device.send_command('configure terminal') then I want to be able to execute more commands line by line.

Below is another script that does what I want but doesn't call IP's from a file nor writes the output to a file.

import paramiko
import time

ip_address = "10.164.178.254"
username = "admin"
password = "XXXXX"

ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(hostname=ip_address,username=username,password=password)

print ("Successful connection", ip_address)

remote_connection = ssh_client.invoke_shell()

remote_connection.send("configure terminal\n")
remote_connection.send("snmp-server community ORION\n")
remote_connection.send("snmp-server host 10.1.4.82 version 2c ORION\n")
remote_connection.send("line vty 0 4\n")
remote_connection.send("exec-timeout 60\n")

remote_connection.send("end\n")

time.sleep(1)
output = remote_connection.recv(65535)
print(output)

ssh_client.close
Can someone please assist in somehow combining the functionality of both of the scripts into one that will accomplish the end goal? Any help is greatly appreciated!
Bump... Any help please?
Hi, you can add this on the second code to pullout the list of the IP addresses.

####OPEN IPADD TXT
fname = r"ipadd.txt"
file = open(fname,'r')
teststr = file.read()
##print ("LIST OF ADDRESSES\n",teststr)