|
| 1 | +from napalm import get_network_driver |
| 2 | +from netmiko.ssh_exception import NetMikoTimeoutException |
| 3 | +from paramiko.ssh_exception import SSHException |
| 4 | +from netmiko.ssh_exception import AuthenticationException |
| 5 | + |
| 6 | + |
| 7 | +def devices(): |
| 8 | + with open('devices') as f: |
| 9 | + devices_list = f.read().splitlines() |
| 10 | + return devices_list |
| 11 | + |
| 12 | + |
| 13 | +def user_pass(): |
| 14 | + username = input("enter your username: ") |
| 15 | + password = input("enter your password: ") |
| 16 | + return username, password |
| 17 | + |
| 18 | + |
| 19 | +def commands(): |
| 20 | + with open('commands') as f: |
| 21 | + commands_to_send = f.read() |
| 22 | + return commands_to_send |
| 23 | + |
| 24 | + |
| 25 | +def main(): |
| 26 | + print("Devices to be configured: ") |
| 27 | + print(devices()) |
| 28 | + print("*" * 100) |
| 29 | + print("Currently loaded configuration:\n") |
| 30 | + print(commands()) |
| 31 | + print("\n") |
| 32 | + |
| 33 | + u, p = user_pass() |
| 34 | + |
| 35 | + for device in devices(): |
| 36 | + |
| 37 | + try: |
| 38 | + driver = get_network_driver('ios') |
| 39 | + ios = driver(device, u, p) |
| 40 | + ios.open() |
| 41 | + |
| 42 | + except (AuthenticationException): |
| 43 | + print('Authentication failure: ' + device) |
| 44 | + continue |
| 45 | + except (NetMikoTimeoutException): |
| 46 | + print('Timeout to device: ' + device) |
| 47 | + continue |
| 48 | + except (EOFError): |
| 49 | + print('End of file while attempting device ' + device) |
| 50 | + continue |
| 51 | + except (SSHException): |
| 52 | + print('SSH Issue. Are you sure SSH is enabled? ' + device) |
| 53 | + continue |
| 54 | + except Exception as unknown_error: |
| 55 | + print('Unspecified error: ' + str(unknown_error)) |
| 56 | + continue |
| 57 | + |
| 58 | + ios_output = ios.get_facts() |
| 59 | + os_version = ios_output.get("os_version", "") |
| 60 | + hostname = ios_output.get("hostname", "") |
| 61 | + fqdn = ios_output.get("fqdn", "") |
| 62 | + |
| 63 | + print("*" * 100) |
| 64 | + print(f"""Accessing |
| 65 | + {hostname} |
| 66 | + {device} |
| 67 | + {fqdn} |
| 68 | + {os_version} |
| 69 | + """) |
| 70 | + |
| 71 | + ios.load_merge_candidate(filename='commands') |
| 72 | + diffs = ios.compare_config() |
| 73 | + |
| 74 | + if len(diffs) > 0: |
| 75 | + print(diffs) |
| 76 | + print("\n") |
| 77 | + print(f"you are conected to: ", {hostname}, {device}) |
| 78 | + ios.commit_config() |
| 79 | + ios.close() |
| 80 | + |
| 81 | + else: |
| 82 | + print("No change is required") |
| 83 | + ios.discard_config() |
| 84 | + ios.close() |
| 85 | + |
| 86 | + |
| 87 | +if __name__ == "__main__": |
| 88 | + main() |
0 commit comments