def run(ipaddr): # Calls function to set current working directory to txtfiles setdir() # To parse the correct file equivalent to the ip address df = pd.read_csv("new_file.csv") df = df.loc[df["host"] == ipaddr] df.set_index("host", inplace=True) fname = "{}_ACL.cfg".format(df.loc[ipaddr, "site_id"]) driver = get_network_driver("ios") np = driver(ipaddr, "admin", "cisco123") try: print("Connecting to {}".format(ipaddr)) # Logs in to cisco ios devices using Napalm np.open() print("Deploying configuration to {}".format(ipaddr)) # Pushes seed file to devices np.load_merge_candidate(filename=fname) np.commit_config() # Closes connection np.close() print("Successfully deployed configuration to {}".format(ipaddr)) # Catches all errors and continues loop except Exception as e: print("{} - {}".format(ipaddr, e)) pass
def graph(country): # Calls function to set current working directory to txtfiles setdir() country = country.upper() df = pd.read_csv("covid19.csv") # Converts date(string) to datetime object df["dates"] = pd.to_datetime(df["dates"]) df.sort_values("dates", inplace=True) # Plots graph x = df["dates"] y1 = df["cases"] y2 = df["deaths"] y3 = df["recoveries"] plt.plot(x, y1, color="y", label="Cases") plt.plot(x, y2, color="r", label="Deaths") plt.plot(x, y3, color="g", label="Recoveries") plt.title("Covid19 Graph") plt.grid() plt.gcf().autofmt_xdate() plt.legend() plt.show()
def push_config(task): setdir() df = pd.read_csv("seed.csv") df = df.loc[df["hostname"] == task.host.name] df.set_index("hostname", inplace=True) fname = "{}_ACL.cfg".format(df.loc[task.host.name, "site_id"]) task.run( task=napalm_configure, replace=False, filename=fname, )
def run(): # Calls function to set current working directory to txtfiles setdir() os.system("clear") start = time.perf_counter() # Loads csv file df = pd.read_csv("new_file.csv") # Loops to each host from new_csv.csv column "host" for index, eachline in enumerate(df["host"]): fname = "{}_ACL.cfg".format(df.loc[index, "site_id"]) driver = get_network_driver("ios") np = driver(eachline, "admin", "cisco123") try: # Logs in to cisco ios devices using Napalm print("Connecting to {}".format(eachline)) np.open() print("Deploying configuration to {}".format(eachline)) # Pushes seed file to devices np.load_merge_candidate(filename=fname) np.commit_config() # Closes connection np.close() print("Successfully deployed configuration to {}".format(eachline)) # Catches all errors and continues loop except Exception as e: print("{} - {}".format(eachline, e)) continue end = time.perf_counter() input("\n\nFinished in {} seconds. Press Enter to continue".format( round(end - start, 3))) os.system("clear")
def generate_seed(): # Calls function to set current working directory to txtfiles setdir() os.system("clear") start = time.perf_counter() # Loads csv file df = pd.read_csv("new_file.csv") for index, eachline in enumerate(df["site_id"]): # Create str filename with site id as reference filename = "{}_ACL.cfg".format(eachline) # Catches error if file is not found # Current file will be renamed with .old extension try: os.rename(filename, "{}.old".format(filename)) except Exception: pass # Converts seed file NETS variables to their equivalent IP from # new_file.csv with open("seed.txt", "r") as f: seed = f.read() seed = seed.replace("NETA.", df.loc[index, "neta"]) seed = seed.replace("NETB.", df.loc[index, "netb"]) seed = seed.replace("NETC.", df.loc[index, "netc"]) # Saves variable seed to file named stored in variable filename with open(filename, "a") as outf: outf.write(seed) print("File saved on {}".format(os.path.join(os.getcwd(), filename))) end = time.perf_counter() input("\n\nFinished in {} seconds. Press Enter to continue".format( round(end - start, 3))) os.system("clear")
def load_seed(): # Calls function to set current working directory to txtfiles setdir() os.system("clear") seed = [] print("Type END to save and quit file\n") while True: _input = input("") # If input is END, breaks loop if _input == "END": break # Appends current input to new input on varible list - seed seed.append(_input) # Saves seed value to seed.txt with open("seed.txt", "w") as outf: for eachline in seed: outf.write("{}\n".format(eachline)) os.system("clear")
def bkup(): os.system("clear") setdir() PATH = os.getcwd() print("Performing backup...") nr = InitNornir("config.yaml") backup_files = nr.run(task=netmiko_send_command, command_string="show run") for each_host in backup_files: if not backup_files[each_host].failed: fname = "{}/{}-bkup.cfg".format(PATH, each_host) config = backup_files[each_host].result nr.run(task=write_file, content=config, filename=fname) print("Saved backup file on {}".format(fname)) else: print("Error saving file for {}. Check {}/nornir.log".format( each_host, PATH))
from loadfile import setdir import signal # Catches keyboard interrupts signal.signal(signal.SIGPIPE, signal.SIG_DFL) signal.signal(signal.SIGINT, signal.SIG_DFL) # Dictionary to build ConnectHandler parameters host = { "device_type": "cisco_ios", "username": credentials.uid(), "password": credentials.pw() } # Calls function to set current working directory to txtfiles setdir() # Extracts device list from H-list.txt file and stores to variable/list device with open("H-list") as f: devices = f.read().strip().splitlines() # Extracts device command from commands.txt file and stores to cmds with open("commands") as f: cmds = f.read().strip().splitlines() for device in devices: try: host["ip"] = device print("Connecting to {}".format(device)) nc = netmiko.ConnectHandler(**host) # Connects to devices for cmd_list in cmds:
import os import loadfile if __name__ == "__main__": # Catches keyboard interrupts signal.signal(signal.SIGPIPE, signal.SIG_DFL) signal.signal(signal.SIGINT, signal.SIG_DFL) # Uses environment variables stored on ~/.bash_profile uid = os.environ.get("RT_USERNAME") pw = os.environ.get("RT_PASS") # Calls function to set current working directory to txtfiles loadfile.setdir() while True: # Test if input to select device is valid try: hostname = loadfile.select_device() except KeyError: continue except ValueError: continue driver = get_network_driver("ios") cisco_ios = driver(hostname, uid, pw)