Netmiko - config-cisco.py.md

from pprint import pprint
from netmiko import ConnectHandler
from time import time
import json, os, threading, sys

def config_worker( ip, username, password ):

    #---- Connect to the device ----
    print ("---- Connecting to device {0}".format( ip ))

    #---- Connect to the device
    session = ConnectHandler( device_type = "cisco_s300", ip = ip, username = username, password = password, global_delay_factor=2 )

    #---- Use CLI command to set configuration data on device
    print ("---- Setting configuration on device")
    config_data = session.config_mode()
    config_data += session.send_command("no ip telnet server",expect_string=r"#",delay_factor=6)
#    config_data += session.send_command_timing("clock timezone " " 0 minutes 0 \n",strip_command = False)
#    config_data += session.send_command_timing("clock summer-time web recurring eu \n",strip_command = False)
#    config_data += session.send_command_timing("clock source sntp \n",strip_command = False)
#    config_data += session.send_command_timing("sntp unicast client enable \n",strip_command = False)
#    config_data += session.send_command_timing("sntp unicast client poll \n",strip_command = False)
#    config_data += session.send_command_timing("sntp server x.x.x.x poll \n",strip_command = False)
#    config_data += session.send_command_timing("logging host x.x.x.x port 6514 \n",strip_command = False)
#    config_data += session.send_command_timing("logging origin-id hostname \n",strip_command = False)
#    config_data += session.send_command_timing("no logging console \n",strip_command = False)
#    config_data += session.send_command_timing("no passwords complexity enable \n",strip_command = False)
#    config_data += session.send_command_timing("passwords aging 0 \n",strip_command = False)
#    config_data += session.send_command_timing("snmp-server server \n",strip_command = False)
#    config_data += session.send_command_timing("snmp-server community public ro x.x.x.x view Default \n",strip_command = False)
#    config_data += session.send_command_timing("no ip http server \n",strip_command = False)
#    config_data += session.send_command_timing("encrypted radius-server key x \n",strip_command = False)
#    config_data += session.send_command_timing("encrypted radius-server host x.x.x.x key x \n",strip_command = False)
#    config_data += session.send_command_timing("ip http authentication aaa login-authentication radius local \n",strip_command = False)
#    config_data += session.send_command_timing("aaa authentication login SSH radius local \n",strip_command = False)
#    config_data += session.send_command_timing("aaa authentication login Telnet local \n",strip_command = False)
#    config_data += session.send_command_timing("aaa authentication login Console local \n",strip_command = False)
#    config_data += session.send_command_timing("aaa accounting login start-stop group radius \n",strip_command = False)
#    config_data += session.send_command_timing("line ssh \n",strip_command = False)
#    config_data += session.send_command_timing("login authentication SSH \n",strip_command = False)
#    config_data += session.send_command_timing("password x encrypted \n",strip_command = False)
#    config_data += session.send_command_timing("exit \n",strip_command = False)
#    config_data += session.exit_config_mode()
#    config_data += session.send_command_timing("wr",strip_command = False)
#    config_data += session.send_command_timing("Y",strip_command = False)
    print (config_data)
    session.disconnect()

    return

#==============================================================================
# ---- Main: Get Configuration
#==============================================================================

#---- Define base path to run from anywhere
basepath = os.path.abspath(os.path.dirname(__file__)) + "/"

#---- Retrieve creds from file
with open(basepath + "creds.json", "r") as f:
    creds = json.load(f)

username = creds["CISCO"]["USERNAME"]
password = creds["CISCO"]["PASSWORD"]

#---- Retrieve list of IPs from file
ips = [line.rstrip("\n") for line in open( basepath + "devices-cisco")]

starting_time = time()

config_threads_list = []

for ip in ips:
    print ("---- Creating thread for: ", ip)
    config_threads_list.append( threading.Thread( target=config_worker, args=( ip, username, password, ) ) )

print ("---- Begin get config threading ----")
for config_thread in config_threads_list:
    config_thread.start()

for config_thread in config_threads_list:
    config_thread.join()

print ("---- End get config threading, elapsed time=", time() - starting_time)