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

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 74 Feb-11-2019, 09:20 PM
Last Post: marienbad
  Run interpreter from Network? JP_ROMANO 3 334 Nov-14-2018, 02:50 AM
Last Post: wavic
  Multiple network socket servers? MuntyScruntfundle 1 257 Nov-13-2018, 03:46 PM
Last Post: wavic
  Pexpect - Python 3.7 - Unable to get my script past the login stage lgreenjr 3 438 Nov-01-2018, 10:49 PM
Last Post: lgreenjr
  Python script hangs after entering username to remote login using telnetlib auto 0 589 Sep-10-2018, 01:10 PM
Last Post: auto
  How to best communicate with android phone from PC running Python script? Sol33t303 1 433 Aug-30-2018, 05:04 AM
Last Post: buran
  safe output in a file for all switches in a network henry1077 0 413 Jul-26-2018, 01:05 PM
Last Post: henry1077
  network programming karan 2 546 Jul-03-2018, 12:49 PM
Last Post: buran
  Batch network configuration/ verification task using Python anonyfate 1 509 Jul-03-2018, 04:30 AM
Last Post: buran
  Sync folder over network Guybrush 3 643 May-15-2018, 09:14 AM
Last Post: wavic

Forum Jump:


Users browsing this thread: 1 Guest(s)