Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Assistance with Python Network script
#1
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!
buran wrote Feb-14-2019, 02:37 PM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Quote
#2
Bump... Any help please?
Quote
#3
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)
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  A virtual network to use with python to learn about networking with python? marienbad 0 444 Feb-11-2019, 09:20 PM
Last Post: marienbad
  Batch network configuration/ verification task using Python anonyfate 1 900 Jul-03-2018, 04:30 AM
Last Post: buran
  How to direct python to use a specific network adapter? ormesome 9 3,715 Nov-08-2017, 06:42 PM
Last Post: nilamo

Forum Jump:


Users browsing this thread: 1 Guest(s)