06-21-2021 08:20 AM
Hi,
It will be very helpful if someone can help with a script (preferably Python) using which I can change/update the hostnames of multiple Cisco devices instead of doing it one by one manually.
I already have the hostname configured just need to update the hostname across 100s of Cisco devices to maintain the standards.
Please note, I am very new to automation and for now, I only know how to SSH into Cisco devices using Python+Netmiko.
Thank you
Shiv
06-21-2021 08:32 AM
06-21-2021 09:38 AM
Hi @inderdeeps, Thank you for your response. I am looking for something simple to understand.
06-21-2021 08:37 AM
Here is Pythion script easy to understand and simple to replace what you like to replace on the devices :
https://github.com/grelleum/youtube-network-automation
check the youtube, his series simple, in hours time you can do what you want.
06-21-2021 09:40 AM
Hi @balaji.bandi, Thank you for your response.
I went through few initial videos. It seems simple and interesting. Let me go through all and see if I find the solution.
Thank you
Shiv
06-21-2021 09:46 AM
if you see all 9 or 10 videos, you become python network programmer can do day to day task.
Good Luck.
06-29-2021 05:36 AM - edited 06-29-2021 05:52 AM
Hi there,
Try the following:
#!/usr/bin/env python3 import getpass import csv import netmiko import paramiko from argparse import ArgumentParser def main(): parser = ArgumentParser(description='Arguments for running oneLiner.py') parser.add_argument('-c', '--csv', required=True, action='store', help='Location of CSV file') args = parser.parse_args() ssh_username = input("SSH username: ") ssh_password = getpass.getpass('SSH Password: ') with open(args.csv, "r") as file: reader = csv.DictReader(file) for device_row in reader: try: ssh_session = netmiko.ConnectHandler(device_type='cisco_ios', ip=device_row['device_ip'], username=ssh_username, password=ssh_password) print("+++++ {0} +++++".format(device_row['device_ip'])) commands = ["hostname {0}".format(device_row['new_hostname'])] ssh_session.enable() ssh_session.send_config_set(commands) ssh_session.send_command("wr mem") ssh_session.disconnect() except (netmiko.ssh_exception.NetMikoTimeoutException, netmiko.ssh_exception.NetMikoAuthenticationException, paramiko.ssh_exception.SSHException) as s_error: print(s_error) if __name__ == "__main__": main()
You will need to feed it a csv file with the following format:
device_ip,new_hostname
192.168.1.1,new_host01 192.168.1.2,new_host02 192.168.1.3,new_host03
# python3 -c new_hostnames.csv
The script will prompt you for a username and password and will use those credentials for every device in the csv file.
cheers,
Seb.
Find answers to your questions by entering keywords or phrases in the Search bar above. New here? Use these resources to familiarize yourself with the community: