-
-
Save twr14152/fd5531d28d3d62b934e26996b590029c to your computer and use it in GitHub Desktop.
#!/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 $ | |
''' |
Thanks, Todd, I will try this and update you, I am very new to coding and started learning Python from yesterday, My role demands it now ;)
Thanks, Todd, I checked the script it worked as expected, it is printing output twice that one fix I have done, also done change in port number instead of 8181 changed to 22.
I want to learn Python for networking and network device in general for IT infra can you recommend anything which will take me to step by step learning beginner to expert
Hi Todd, I am using a script by doing changes in command however while doing this I realize it will be more productive if the script should be standard and we should provide a 1) external txt file where we can mention commands and IP /hostname txt file. 2) before creating output file script should check desired word setting is there in out file (desired out can be written in txt file like IP and command mentioned in point 1) 3) if desired out for that command is not there then output file should not create and that IP should be written in the different output file. I can think of this logic looking at my work requirement but not yet skilled to create my own pls suggest.
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.
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.
This example was tested against cisco devnet gear and may need to have the nonstandard ssh port commands removed. Hope this works for you.