Created
July 29, 2020 23:04
-
-
Save twr14152/fd5531d28d3d62b934e26996b590029c to your computer and use it in GitHub Desktop.
requested_ios_config_script_07292020
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
#(c) 2020 Todd Riemenschneider | |
# | |
#Enable Multiprocessing | |
from multiprocessing import Pool | |
#getpass will not display password | |
from getpass import getpass | |
#ConnectionHandler is the function used by netmiko to connect to devices | |
from netmiko import ConnectHandler | |
#Time tracker | |
from time import time | |
#create variables for username and password | |
#create variables for configs and hosts | |
uname = input("Username: ") | |
passwd = getpass("Password: ") | |
#This will allow you to just press enter | |
#This sets default values Not recommanded in any place but a lab | |
if len(uname) < 1 : uname = "developer" | |
if len(passwd) < 1 : passwd = "C1sco12345" | |
# Pull target hosts from host_file | |
with open('host_file.txt') as f: | |
hosts = f.read().splitlines() | |
config_cmd = ["ip http client connection forceclose",] | |
commands = 'show ip http client all' | |
starting_time = time() | |
#Each member of the pool of 5 will be run through this function | |
def run_script(host_ip): | |
ios_rtr = { | |
"device_type": "cisco_ios", | |
"host": host_ip, | |
"port": 8181, | |
"username": uname, | |
"password": passwd, | |
} | |
nl = "\n" | |
try: | |
#Connect to the device via ssh | |
net_connect = ConnectHandler(**ios_rtr) | |
host_name = net_connect.find_prompt() | |
print("Connected to host:", host_ip) | |
host_id = "Connected to host: " + host_ip | |
print('\n---- Elapsed time=', time()-starting_time) | |
cmd_output = net_connect.send_command(commands) | |
print(f"Pre-config state:{cmd_output}") | |
cfg_output = net_connect.send_config_set(config_cmd) | |
print(f"Config:{cmd_output}") | |
cmd_output2 = net_connect.send_command(commands) | |
print(f"Post-config state:{cmd_output2}") | |
print(cfg_output) | |
with open(host_ip + "_.txt", 'a') as file: | |
file.write(host_id) | |
file.write(nl) | |
file.write(host_name) | |
file.write(nl) | |
file.write(cmd_output) | |
file.write(nl) | |
file.write(str(config_cmd)) | |
file.write(nl) | |
file.write(cfg_output) | |
file.write(nl) | |
file.write(cmd_output2) | |
file.write(nl) | |
file.write("**************************************") | |
file.write(nl) | |
except Exception as unknown_error: | |
# Error handling - Print output to screen | |
print("************************************") | |
print("Unable to log into this device:", host_ip) | |
print(unknown_error) | |
print("************************************") | |
# Error handling - record to file | |
with open("Connection_Errors", "a") as err_log: | |
err_log.write("Error connecting to the following devices") | |
err_log.write(nl) | |
err_log.write(str(unknown_error)) | |
err_log.write(nl) | |
err_log.write(host_ip) | |
err_log.write(nl) | |
if __name__ == "__main__": | |
# Pool(5) means 5 process will be run at a time, more hosts will go in the next group | |
with Pool(5) as p: | |
print(p.map(run_script, hosts)) | |
''' | |
You will need to create and host_file.txt. | |
pi@RaspPi4:~/Coding/Python_folder/scripts $ cat host_file.txt | |
ios-xe-mgmt.cisco.com | |
ios-xe-mgmt-latest.cisco.com | |
pi@RaspPi4:~/Coding/Python_folder/scripts $ | |
''' |
Hey, Sorry for the late response. If you know the command line syntax of the huawei routers you could use netmiko. In my mind its the best network automation library to start with. Basically you could take a any multi-device netmiko script change the device-type to huawei. Tweak any command syntax differences. Lastly use a for loop and simply roll through your devices.
Get the script working to one device then work on adding the others. Keep it simple.
I would think this could get you started as far as what a simple script looks like.
Huawei specific use of netmiko example:
- ktbyers/netmiko#902
Netmiko General info:
https://pynet.twb-tech.com/blog/automation/netmiko.html
Good luck sorry I couldn't help more got some other things I have to get done.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi Todd, need your ideas how to do it in huawei routers. I want to ssh multiple huawei routers and run multiple commands.
I'm just also new in programming. Thanks in advance.