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
  Issues in Chat App for Local Network iMuny 2 202 Aug-19-2019, 10:25 AM
Last Post: iMuny
  Server and Network (socket) [WinError 10053] Timxxx 0 118 Jul-17-2019, 11:50 PM
Last Post: Timxxx
  creation of an application for lora sever network moez 3 312 Apr-18-2019, 11:05 AM
Last Post: buran
  Server and Network (socket) [WinError 10053] SheeppOSU 2 558 Apr-13-2019, 09:23 PM
Last Post: SheeppOSU
  Need to build a ping test for work network austina2 2 617 Mar-11-2019, 06:58 PM
Last Post: austina2
  How to run local python script to remote machine without sending krishna1989 1 681 Feb-21-2019, 05:18 PM
Last Post: marienbad
  A virtual network to use with python to learn about networking with python? marienbad 0 313 Feb-11-2019, 09:20 PM
Last Post: marienbad
  Run interpreter from Network? JP_ROMANO 3 623 Nov-14-2018, 02:50 AM
Last Post: wavic
  Multiple network socket servers? MuntyScruntfundle 1 500 Nov-13-2018, 03:46 PM
Last Post: wavic
  Pexpect - Python 3.7 - Unable to get my script past the login stage lgreenjr 3 881 Nov-01-2018, 10:49 PM
Last Post: lgreenjr

Forum Jump:


Users browsing this thread: 1 Guest(s)