· 5 years ago · Sep 13, 2020, 04:26 AM
1import os
2import cli
3import time
4import sys
5import csv
6
7"""
8# sample usage of python cli module
9# cli.executep("show ver") or cli.execute("show ver") - single command
10# cli.cli("show ver; show clock") - can do several commands
11# cli.configure or cli.configurep(["interface GigabitEthernet0/1", "no shutdown", "end"])
12"""
13
14def print_line (text, step=None, width=40):
15 stext="({})".format(step) if step else "***"
16 if step:
17 print("\n")
18 print("***{}*** {} {}".format(stext, text, "*"*(width-len(text))))
19
20"""
21#####Install necessary python modules/libraries#####
22"""
23
24os.system("sudo python3 -m pip install --upgrade pip")
25os.system("sudo python3 -m pip install --upgrade setuptools")
26os.system("sudo python3 -m pip install webexteamssdk")
27os.system("sudo python3 -m pip install requests")
28os.system("sudo python3 -m pip install rich")
29os.system("sudo python3 -m pip install meraki")
30os.system("sudo python3 -m pip install python-dotenv")
31os.system("sudo yum -y install ipython3")
32
33"""
34#####Import additional modules/libraries
35"""
36
37from rich import print
38from dotenv import load_dotenv
39from webexteamssdk import WebexTeamsAPI
40
41"""
42#####Start of main workflow
43"""
44
45tftp_server = "1.1.1.1"
46print_line("Downloading files",0)
47cli.executep(f"copy tftp://{tftp_server}/.env bootflash:/guest-share/.env")
48work_dir = '/bootflash/guest-share'
49env_path = '.env'
50load_dotenv(dotenv_path=os.path.join(work_dir,env_path))
51api = WebexTeamsAPI(access_token=os.getenv("WEBEX_TEAMS_ACCESS_TOKEN"))
52print_line("Checking hardware", 1)
53output = cli.execute("show platform | i (Model|C93)")
54print(output)
55api.messages.create(roomId=os.getenv("WEBEX_ROOMID"),text=output)
56print_line("Checking IOS version", 2)
57output = cli.execute("show version | i IOS XE")
58print(output)
59api.messages.create(roomId=os.getenv("WEBEX_ROOMID"),text=output)
60print_line("Generating RSA key", 3)
61output = cli.configure("crypto key generate rsa modulus 2048")
62print(output)
63api.messages.create(roomId=os.getenv("WEBEX_ROOMID"),text=output)
64print_line("Obtaining serial number", 4)
65license = cli.cli("show license udi")
66sn = license.split(":")[3].rstrip()
67output = "Serial number is {}".format(sn)
68print_line(output)
69api.messages.create(roomId=os.getenv("WEBEX_ROOMID"),text=output)
70print_line("Disabling copy prompts", 5)
71output = cli.configure("file prompt quiet")
72print(output)
73api.messages.create(roomId=os.getenv("WEBEX_ROOMID"),text=output)
74print_line("Copying configuration file from TFTP server", 6)
75cli_command = "copy tftp://{}/config/{}-confg startup-config vrf Mgmt-vrf".format(tftp_server, sn.lower())
76cli.executep(cli_command)
77time.sleep (5)
78
79try:
80 print_line("Verifying received startup config...", 7)
81 host_line=cli.cli("show startup-config | i hostname").split()
82 if host_line:
83 host_name=host_line[-1]
84 if not "switch" in host_name:
85 output = "Configuration for {} downloaded successfully!".format(host_name)
86 print(output)
87 api.messages.create(roomId=os.getenv("WEBEX_ROOMID"),text=output)
88 else:
89 output = "************Startup-Config Download Failed**************"
90 print(output)
91 api.messages.create(roomId=os.getenv("WEBEX_ROOMID"),text=output)
92 output = "*****************Erasing Startup-Config*****************"
93 print(output)
94 cli_command = "erase startup-config"
95 output = cli.executep(cli_command)
96 api.messages.create(roomId=os.getenv("WEBEX_ROOMID"),text=output)
97 output = "***************Cleaning Up Temp Files*******************"
98 print_line("Cleaning up Temp Files", 9)
99 cli.execute("delete /r /f bootflash:/guest-share/.env")
100 api.messages.create(roomId=os.getenv("WEBEX_ROOMID"),text=output)
101 print_line("Rebooting with the new config!", 9)
102 output = "*************Rebooting with the new config**************"
103 api.messages.create(roomId=os.getenv("WEBEX_ROOMID"),text=output)
104 cli.cli("reload")
105
106except Exception("Something Went Wrong") as e:
107 print(e)
108