├── Old ├── network_automation │ ├── README.md │ ├── os_vendor.py │ ├── credentials.py │ ├── IPAddressList.txt │ ├── Commands.txt │ ├── test.py │ ├── Show_Command_old.py │ └── Show_Commands_wPing.py ├── test.py └── Show_Command_old.py ├── .DS_Store ├── example1.png ├── IPAddressList.txt ├── __pycache__ ├── ping.cpython-38.pyc ├── type.cpython-310.pyc ├── type.cpython-38.pyc ├── os_vendor.cpython-310.pyc ├── os_vendor.cpython-38.pyc ├── credentials.cpython-310.pyc └── credentials.cpython-38.pyc ├── Commands.txt ├── README.md ├── static_show_commands_create.py ├── static_show_commands_append.py ├── dynamic_show_commands_create.py ├── dynamic_show_commands_append.py └── test.py /Old/network_automation/README.md: -------------------------------------------------------------------------------- 1 | WIP 2 | -------------------------------------------------------------------------------- /Old/network_automation/os_vendor.py: -------------------------------------------------------------------------------- 1 | type_ios = 'cisco_ios' -------------------------------------------------------------------------------- /Old/network_automation/credentials.py: -------------------------------------------------------------------------------- 1 | username = 'cisco' 2 | password = 'cisco' -------------------------------------------------------------------------------- /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thetechguy-it/Cisco_ShowCommands/HEAD/.DS_Store -------------------------------------------------------------------------------- /example1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thetechguy-it/Cisco_ShowCommands/HEAD/example1.png -------------------------------------------------------------------------------- /IPAddressList.txt: -------------------------------------------------------------------------------- 1 | 192.168.37.101 2 | 192.168.37.102 3 | 192.168.37.103 4 | 192.168.37.105 5 | 192.168.37.104 -------------------------------------------------------------------------------- /__pycache__/ping.cpython-38.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thetechguy-it/Cisco_ShowCommands/HEAD/__pycache__/ping.cpython-38.pyc -------------------------------------------------------------------------------- /__pycache__/type.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thetechguy-it/Cisco_ShowCommands/HEAD/__pycache__/type.cpython-310.pyc -------------------------------------------------------------------------------- /__pycache__/type.cpython-38.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thetechguy-it/Cisco_ShowCommands/HEAD/__pycache__/type.cpython-38.pyc -------------------------------------------------------------------------------- /__pycache__/os_vendor.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thetechguy-it/Cisco_ShowCommands/HEAD/__pycache__/os_vendor.cpython-310.pyc -------------------------------------------------------------------------------- /__pycache__/os_vendor.cpython-38.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thetechguy-it/Cisco_ShowCommands/HEAD/__pycache__/os_vendor.cpython-38.pyc -------------------------------------------------------------------------------- /__pycache__/credentials.cpython-310.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thetechguy-it/Cisco_ShowCommands/HEAD/__pycache__/credentials.cpython-310.pyc -------------------------------------------------------------------------------- /__pycache__/credentials.cpython-38.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thetechguy-it/Cisco_ShowCommands/HEAD/__pycache__/credentials.cpython-38.pyc -------------------------------------------------------------------------------- /Old/network_automation/IPAddressList.txt: -------------------------------------------------------------------------------- 1 | 10.155.233.205 2 | 10.155.233.201 3 | 10.155.233.202 4 | 10.155.233.203 5 | 10.155.233.204 6 | 10.155.233.206 -------------------------------------------------------------------------------- /Commands.txt: -------------------------------------------------------------------------------- 1 | show version 2 | show interface status 3 | show ip interface brief 4 | show run | inc domain 5 | show run | inc ntp 6 | show ip route 7 | show run -------------------------------------------------------------------------------- /Old/network_automation/Commands.txt: -------------------------------------------------------------------------------- 1 | show version 2 | show interface status 3 | show ip interface brief 4 | show run | inc domain 5 | show run | inc ntp 6 | show ip route 7 | show run -------------------------------------------------------------------------------- /Old/test.py: -------------------------------------------------------------------------------- 1 | from ping3 import ping 2 | from datetime import date, datetime 3 | 4 | now = datetime.now() 5 | dt_string_full = now.strftime("%m-%d-%Y_%H-%M") 6 | dt_string = now.strftime("%m-%d-%Y") 7 | 8 | ip = "10.155.233.205" 9 | ip_reach = ping(ip) 10 | print(ip_reach) 11 | if ip_reach == None: 12 | print("The device with the IP Address: " + ip + " is not reachable.") 13 | with open(ip + "_" + dt_string_full + ".txt", "w") as downdevice: 14 | downdevice.write("This device is not reachable, please fix the issue") 15 | else: 16 | print("It is reachable") 17 | 18 | ip1 = "10.155.233.201" 19 | ip_reach = ping(ip1) 20 | print(ip_reach) 21 | if ip_reach == None: 22 | print("The device with the IP Address: " + ip1 + " is not reachable.") 23 | with open(ip1 + "_" + dt_string_full + ".txt", "w") as downdevice: 24 | downdevice.write("This device is not reachable, please fix the issue") 25 | else: 26 | print(ip1 + " is reachable") -------------------------------------------------------------------------------- /Old/network_automation/test.py: -------------------------------------------------------------------------------- 1 | from ping3 import ping 2 | from datetime import date, datetime 3 | 4 | now = datetime.now() 5 | dt_string_full = now.strftime("%m-%d-%Y_%H-%M") 6 | dt_string = now.strftime("%m-%d-%Y") 7 | 8 | ip = "10.155.233.205" 9 | ip_reach = ping(ip) 10 | print(ip_reach) 11 | if ip_reach == None: 12 | print("The device with the IP Address: " + ip + " is not reachable.") 13 | with open(ip + "_" + dt_string_full + ".txt", "w") as downdevice: 14 | downdevice.write("This device is not reachable, please fix the issue") 15 | else: 16 | print("It is reachable") 17 | 18 | ip1 = "10.155.233.201" 19 | ip_reach = ping(ip1) 20 | print(ip_reach) 21 | if ip_reach == None: 22 | print("The device with the IP Address: " + ip1 + " is not reachable.") 23 | with open(ip1 + "_" + dt_string_full + ".txt", "w") as downdevice: 24 | downdevice.write("This device is not reachable, please fix the issue") 25 | else: 26 | print(ip1 + " is reachable") -------------------------------------------------------------------------------- /Old/Show_Command_old.py: -------------------------------------------------------------------------------- 1 | # With this script you will launch the commands inside the file called 'Commands.txt' to all the devices inside the file called 'IPAddressList.txt 2 | 3 | from netmiko import ConnectHandler 4 | import credentials 5 | import os_vendor 6 | import os 7 | from datetime import date, datetime 8 | 9 | total_ips = open("IPAddressList.txt", "r") 10 | ips = total_ips.readlines() 11 | total_commands = open("Commands.txt", "r") 12 | commands = total_commands.readlines() 13 | 14 | print("With this script you will launch the commands inside the file called 'Commands.txt' to all the devices inside the file called 'IPAddressList.txt") 15 | 16 | # Checks if the backup folder exists, if not, it creates it. 17 | backup_folder = "CFG_Backup" 18 | if not os.path.exists(backup_folder): 19 | os.mkdir(backup_folder) 20 | os.chdir(backup_folder) 21 | 22 | # Current time and formats it as: Month, Day, Year, Hour and Minute. 23 | now = datetime.now() 24 | dt_string_full = now.strftime("%m-%d-%Y_%H-%M") 25 | dt_string = now.strftime("%m-%d-%Y") 26 | 27 | # Checks if the daily folder exists, if not, it creates it. 28 | if not os.path.exists(dt_string): 29 | os.mkdir(dt_string) 30 | os.chdir(dt_string) 31 | 32 | for ip in ips: 33 | SSH = ConnectHandler (ip, device_type = os_vendor.type_ios, username = credentials.username, password = credentials.password, fast_cli = False) 34 | dev_name = SSH.send_command("show run | in hostname") 35 | dev_name = dev_name.split(" ") 36 | hostname = dev_name[1] 37 | print ("I'm processing the device called ", hostname, "with the following IP Address:", ip) 38 | SSH_Check_Connection = SSH.is_alive() 39 | print("The connection to the device is:" + str(SSH_Check_Connection)) 40 | with open(hostname + "_" + dt_string_full + ".txt", "w") as txt_file: 41 | print("The file called: " + hostname + ".txt has been created into the " + backup_folder + "folder. You will find here all the outputs") 42 | for command in commands: 43 | txt_file.write("This is the output for the following command:" + command) 44 | output = (SSH.send_command(command)) 45 | txt_file.write(output + "\n\n") 46 | SSH.disconnect() 47 | SSH_Check_Connection = SSH.is_alive() 48 | print("The connection to the device is:" + str(SSH_Check_Connection)) 49 | print("\n\n\n") 50 | total_ips.close() 51 | total_commands.close() -------------------------------------------------------------------------------- /Old/network_automation/Show_Command_old.py: -------------------------------------------------------------------------------- 1 | # With this script you will launch the commands inside the file called 'Commands.txt' to all the devices inside the file called 'IPAddressList.txt 2 | 3 | from netmiko import ConnectHandler 4 | import credentials 5 | import os_vendor 6 | import os 7 | from datetime import date, datetime 8 | 9 | total_ips = open("IPAddressList.txt", "r") 10 | ips = total_ips.readlines() 11 | total_commands = open("Commands.txt", "r") 12 | commands = total_commands.readlines() 13 | 14 | print("With this script you will launch the commands inside the file called 'Commands.txt' to all the devices inside the file called 'IPAddressList.txt") 15 | 16 | # Checks if the backup folder exists, if not, it creates it. 17 | backup_folder = "CFG_Backup" 18 | if not os.path.exists(backup_folder): 19 | os.mkdir(backup_folder) 20 | os.chdir(backup_folder) 21 | 22 | # Current time and formats it as: Month, Day, Year, Hour and Minute. 23 | now = datetime.now() 24 | dt_string_full = now.strftime("%m-%d-%Y_%H-%M") 25 | dt_string = now.strftime("%m-%d-%Y") 26 | 27 | # Checks if the daily folder exists, if not, it creates it. 28 | if not os.path.exists(dt_string): 29 | os.mkdir(dt_string) 30 | os.chdir(dt_string) 31 | 32 | for ip in ips: 33 | SSH = ConnectHandler (ip, device_type = os_vendor.type_ios, username = credentials.username, password = credentials.password, fast_cli = False) 34 | dev_name = SSH.send_command("show run | in hostname") 35 | dev_name = dev_name.split(" ") 36 | hostname = dev_name[1] 37 | print ("I'm processing the device called ", hostname, "with the following IP Address:", ip) 38 | SSH_Check_Connection = SSH.is_alive() 39 | print("The connection to the device is:" + str(SSH_Check_Connection)) 40 | with open(hostname + "_" + dt_string_full + ".txt", "w") as txt_file: 41 | print("The file called: " + hostname + ".txt has been created into the " + backup_folder + "folder. You will find here all the outputs") 42 | for command in commands: 43 | txt_file.write("This is the output for the following command:" + command) 44 | output = (SSH.send_command(command)) 45 | txt_file.write(output + "\n\n") 46 | SSH.disconnect() 47 | SSH_Check_Connection = SSH.is_alive() 48 | print("The connection to the device is:" + str(SSH_Check_Connection)) 49 | print("\n\n\n") 50 | total_ips.close() 51 | total_commands.close() -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## CODE MISSION 2 | With this code I want to help all the engineers who have to backup configurations, store or retrieve data from multiple devices into a single or multiple devices-named files. Thanks to this code you'll be able to push a defined list of commands to a static or dynamic list of IP addresses. 3 | 4 | There are two versions: 5 | - **Static**: The code will use the list of the IP Addresses defined inside the file called "IPAddressList.txt". Please put one IP per line. 6 | - **Dynamic**: The code will ask you which network you want to scan and he creates a list with all the reachable devices (via ICMP) in the provided network. 7 | 8 | Each version has two different ways to store the data: 9 | - **Create**: The code will create one file for each device 10 | - **Append**: The code will create a single file with all the outputs 11 | 12 | It's up to you! 13 | 14 | 15 | **Static code list of operations**: 16 | - Open the file called "IPAddressList.txt" and store it in a list 17 | - Open the file called "Commands.txt" and store it in a list 18 | - Verify if the devices in the IPAddressList are reachable via ICMP 19 | - SSH connection to each IP 20 | - Push commands (stored in the Commands list) 21 | - Store outputs in dedicated files (create version) o unique file (append version) 22 | 23 | **Dynamic code list of operations**: 24 | - Scan a network provided by the user (via ICMP, it's automatic) 25 | - Store the reachable IP in a list 26 | - SSH connection to each IP. If it's not a Cisco device skip it. 27 | - Push commands (stored in the Commands list) 28 | - Store outputs in dedicated files (create version) o unique file (append version) 29 | 30 | ## MODULES 31 | Install the following modules: 32 | 33 | > pip3 install netmiko 34 | > pip3 install ping3 35 | > pip3 install networkscan 36 | 37 | ## USERNAME/PASSWORD 38 | The code does not have any pre-defined username/password. When you run the code it will ask you the username and password for the devices. 39 | 40 | ## CISCO VERSION 41 | Tested and working with: 42 | - IOS XE 43 | - NX-OS 44 | - IOS 45 | 46 | ## RUN THE CODE 47 | Create a virtual environment in order to test the code: 48 | 49 | > python3 -m venv TEST 50 | > source TEST/bin/activate 51 | > cd TEST/ 52 | > git clone https://github.com/thetechguy-it/show_commands.git 53 | > cd show_commands/ 54 | > pip3 install netmiko 55 | > pip3 install ping3 56 | > pip3 install networkscan 57 | > sudo python3 < scriptname >.py 58 | 59 | The code will create a folder called "BACKUP" and then another folder with the date. Inside this second folder, you will find all your files. 60 | 61 | Example: 62 | ![example1](https://github.com/thetechguy-it/show_commands/blob/main/example1.png) -------------------------------------------------------------------------------- /static_show_commands_create.py: -------------------------------------------------------------------------------- 1 | from netmiko import ConnectHandler 2 | import os 3 | from datetime import date, datetime 4 | from ping3 import ping 5 | import getpass 6 | 7 | def create_folder(folder): 8 | if not os.path.exists(folder): 9 | os.mkdir(folder) 10 | os.chdir(folder) 11 | 12 | # Login to the devices 13 | def device_login(host, username, password): 14 | cisco_ios = { 15 | 'device_type': 'cisco_ios', 16 | 'host': host, 17 | 'username': username, 18 | 'password': password, 19 | } 20 | try: 21 | connection = ConnectHandler(**cisco_ios) 22 | connection.enable() 23 | device_hostname(connection) 24 | except Exception as err: 25 | print(f"Oops! {err}") 26 | 27 | # Retrieve the device Hostname 28 | def device_hostname(SSH): 29 | output = SSH.send_command("show run") 30 | dev_name = SSH.send_command("show run | in hostname") 31 | dev_name = dev_name.split(" ") 32 | hostname = dev_name[1] 33 | print("The device name is: ", hostname) 34 | device_commands(hostname, SSH) 35 | 36 | # Push the commands stored into the "Commands.txt" file and store the output in different files called with Device hostname and timestamp 37 | def device_commands(device_name, device_SSH): 38 | with open(device_name + "_" + dt_string_full + ".txt", "w") as txt_file: 39 | print("The file called: " + device_name + ".txt has been created into the " + backup_folder + "/" + dt_string + " folder. You will find here all the outputs. I'm going to close the SSH session") 40 | for command in commands_list: 41 | txt_file.write("######## " + command + " ######## \n\n") 42 | output = (device_SSH.send_command(command)) 43 | txt_file.write(output + "\n\n\n\n") 44 | device_SSH.disconnect() 45 | print("\n\n\n") 46 | 47 | # Verify if an IP address is reachable. 48 | # If YES: run "device_login" function 49 | # If NO: Create a file called with the IP Address of the device and write inside "This device is not reachable. Please verify its status and connectivity" 50 | def ip_reachability(): 51 | for ip in ip_list: 52 | print("Testing: " + ip) 53 | ip_reach = ping(ip) 54 | if ip_reach == None: 55 | print("The device with the IP Address: " + ip + " is not reachable. Please verify its status and connectivity\n\n\n") 56 | with open(ip + "_" + dt_string_full + ".txt", "w") as downdevice: 57 | downdevice.write("This device is not reachable. Please verify its status and connectivity") 58 | else: 59 | print ("It's reachable! I'm going to retrieve data from it.") 60 | device_login(ip, username, password) 61 | 62 | 63 | # User enters device credentials 64 | username = input('Enter your username: ') 65 | password = getpass.getpass('Enter your password: ') 66 | 67 | # Code opens the file where IP Addresses are stored (1 per line) 68 | file_ip = open("IPAddressList.txt", "r") 69 | total_ips = file_ip.read() 70 | ip_list = total_ips.splitlines() 71 | 72 | # Code opens the file where commands are stored (1 per line) 73 | file_commands = open("Commands.txt", "r") 74 | total_commands = file_commands.read() 75 | commands_list = total_commands.splitlines() 76 | 77 | # Create Backup Folder 78 | backup_folder = "BACKUP" 79 | create_folder(backup_folder) 80 | 81 | # Create daily folder inside Backup Folder 82 | now = datetime.now() 83 | dt_string = now.strftime("%m-%d-%Y") 84 | dt_string_full = now.strftime("%m-%d-%Y_%H-%M-%S") 85 | create_folder(dt_string) 86 | 87 | # Code 88 | ip_reachability() -------------------------------------------------------------------------------- /static_show_commands_append.py: -------------------------------------------------------------------------------- 1 | from netmiko import ConnectHandler 2 | import os 3 | from datetime import date, datetime 4 | from ping3 import ping 5 | import getpass 6 | 7 | def create_folder(folder): 8 | if not os.path.exists(folder): 9 | os.mkdir(folder) 10 | os.chdir(folder) 11 | 12 | # Login to the devices 13 | def device_login(host, username, password): 14 | cisco_ios = { 15 | 'device_type': 'cisco_ios', 16 | 'host': host, 17 | 'username': username, 18 | 'password': password, 19 | } 20 | try: 21 | connection = ConnectHandler(**cisco_ios) 22 | connection.enable() 23 | device_hostname(connection) 24 | except Exception as err: 25 | print(f"Oops! {err}") 26 | 27 | # Retrieve the device Hostname 28 | def device_hostname(SSH): 29 | output = SSH.send_command("show run") 30 | dev_name = SSH.send_command("show run | in hostname") 31 | dev_name = dev_name.split(" ") 32 | hostname = dev_name[1] 33 | print("The device name is: ", hostname) 34 | device_commands(hostname, SSH) 35 | 36 | # Push the commands stored into the "Commands.txt" file and store the output in an unique file called with the actual date 37 | def device_commands(device_name, device_SSH): 38 | txt_file = open(dt_string_full + ".txt", "a") 39 | txt_file.write("#####" + device_name + "#####" + device_name + "#####" + device_name + "#####" + device_name + "#####" + device_name + "\n") 40 | for command in commands_list: 41 | txt_file.write("######## " + command + " ######## \n\n") 42 | output = (device_SSH.send_command(command)) 43 | txt_file.write(output + "\n\n") 44 | device_SSH.disconnect() 45 | SSH_Check_Connection = device_SSH.is_alive() 46 | txt_file.write("\n\n\n\n\n\n\n\n ######################################################################################## \n\n\n\n\n\n\n\n") 47 | print("\n\n\n") 48 | 49 | # Verify if an IP address is reachable. 50 | # If YES: run "device_login" function 51 | # If NO: Create a file called with the IP Address of the device and write inside "This device is not reachable. Please verify its status and connectivity" 52 | def ip_reachability(): 53 | for ip in ip_list: 54 | print("Testing: " + ip) 55 | ip_reach = ping(ip) 56 | if ip_reach == None: 57 | print("The device with the IP Address: " + ip + " is not reachable. Please verify its status and connectivity\n\n\n") 58 | with open(ip + "_" + dt_string_full + ".txt", "w") as downdevice: 59 | downdevice.write("This device is not reachable. Please verify its status and connectivity") 60 | else: 61 | print ("It's reachable! I'm going to retrieve data from it.") 62 | device_login(ip, username, password) 63 | 64 | 65 | # User enters device credentials 66 | username = input('Enter your username: ') 67 | password = getpass.getpass('Enter your password: ') 68 | 69 | # Code opens the file where IP Addresses are stored (1 per line) 70 | file_ip = open("IPAddressList.txt", "r") 71 | total_ips = file_ip.read() 72 | ip_list = total_ips.splitlines() 73 | 74 | # Code opens the file where commands are stored (1 per line) 75 | file_commands = open("Commands.txt", "r") 76 | total_commands = file_commands.read() 77 | commands_list = total_commands.splitlines() 78 | 79 | # Create Backup Folder 80 | backup_folder = "BACKUP" 81 | create_folder(backup_folder) 82 | 83 | # Create daily folder inside Backup Folder 84 | now = datetime.now() 85 | dt_string = now.strftime("%m-%d-%Y") 86 | dt_string_full = now.strftime("%m-%d-%Y_%H-%M-%S") 87 | create_folder(dt_string) 88 | 89 | # Code 90 | ip_reachability() -------------------------------------------------------------------------------- /dynamic_show_commands_create.py: -------------------------------------------------------------------------------- 1 | from netmiko import ConnectHandler 2 | import os 3 | from datetime import date, datetime 4 | from ping3 import ping 5 | import getpass 6 | import networkscan 7 | 8 | def create_folder(folder): 9 | if not os.path.exists(folder): 10 | os.mkdir(folder) 11 | os.chdir(folder) 12 | 13 | # Login to the devices 14 | def device_login(host, username, password): 15 | cisco_ios = { 16 | 'device_type': 'cisco_ios', 17 | 'host': host, 18 | 'username': username, 19 | 'password': password, 20 | } 21 | try: 22 | connection = ConnectHandler(**cisco_ios) 23 | connection.enable() 24 | device_hostname(connection) 25 | except Exception as err: 26 | print(f"Oops! {err}\n") 27 | 28 | # Retrieve the device Hostname 29 | def device_hostname(SSH): 30 | output = SSH.send_command("show run") 31 | dev_name = SSH.send_command("show run | in hostname") 32 | dev_name = dev_name.split(" ") 33 | hostname = dev_name[1] 34 | print("The device name is: ", hostname) 35 | device_commands(hostname, SSH) 36 | 37 | # Push the commands stored into the "Commands.txt" file and store the output in different files called with Device hostname and timestamp 38 | def device_commands(device_name, device_SSH): 39 | with open(device_name + "_" + dt_string_full + ".txt", "w") as txt_file: 40 | print("The file called: " + device_name + ".txt has been created into the " + backup_folder + "/" + dt_string + " folder. You will find here all the outputs. I'm going to close the SSH session") 41 | for command in commands_list: 42 | txt_file.write("######## " + command + " ######## \n\n") 43 | output = (device_SSH.send_command(command)) 44 | txt_file.write(output + "\n\n\n\n") 45 | device_SSH.disconnect() 46 | print("\n") 47 | 48 | # Verify if an IP address is reachable. 49 | # If YES: run "device_login" function 50 | # If NO: Create a file called with the IP Address of the device and write inside "This device is not reachable. Please verify its status and connectivity" 51 | def ip_reachability(ip_list): 52 | for ip in ip_list: 53 | print("Testing: " + ip) 54 | ip_reach = ping(ip) 55 | if ip_reach == None: 56 | print("The device with the IP Address: " + ip + " is not reachable. Please verify its status and connectivity\n\n\n") 57 | with open(ip + "_" + dt_string_full + ".txt", "w") as downdevice: 58 | downdevice.write("This device is not reachable. Please verify its status and connectivity") 59 | else: 60 | print ("It's reachable! I'm going to retrieve data from it.") 61 | device_login(ip, username, password) 62 | 63 | def discover_network(): 64 | network_mask = input("Enter the management network (x.x.x.x/y): ") 65 | my_scan = networkscan.Networkscan(network_mask) 66 | my_scan.run() 67 | reachable_ip = [] 68 | print("List of reachable devices:\n" ) 69 | for i in my_scan.list_of_hosts_found: 70 | print(i) 71 | reachable_ip.append(i) 72 | ip_reachability(reachable_ip) 73 | 74 | # User enters device credentials 75 | username = input('Enter your AD username: ') 76 | password = getpass.getpass('Enter your AD password: ') 77 | 78 | # Code opens the file where commands are stored (1 per line) 79 | file_commands = open("Commands.txt", "r") 80 | total_commands = file_commands.read() 81 | commands_list = total_commands.splitlines() 82 | 83 | # Create Backup Folder 84 | backup_folder = "BACKUP" 85 | create_folder(backup_folder) 86 | 87 | # Create daily folder inside Backup Folder 88 | now = datetime.now() 89 | dt_string = now.strftime("%m-%d-%Y") 90 | dt_string_full = now.strftime("%m-%d-%Y_%H-%M-%S") 91 | create_folder(dt_string) 92 | 93 | # Code 94 | discover_network() -------------------------------------------------------------------------------- /dynamic_show_commands_append.py: -------------------------------------------------------------------------------- 1 | from netmiko import ConnectHandler 2 | import os 3 | from datetime import date, datetime 4 | from ping3 import ping 5 | import getpass 6 | import networkscan 7 | 8 | def create_folder(folder): 9 | if not os.path.exists(folder): 10 | os.mkdir(folder) 11 | os.chdir(folder) 12 | 13 | # Login to the devices 14 | def device_login(host, username, password): 15 | cisco_ios = { 16 | 'device_type': 'cisco_ios', 17 | 'host': host, 18 | 'username': username, 19 | 'password': password, 20 | } 21 | try: 22 | connection = ConnectHandler(**cisco_ios) 23 | connection.enable() 24 | device_hostname(connection) 25 | except Exception as err: 26 | print(f"Oops! {err}\n") 27 | 28 | # Retrieve the device Hostname 29 | def device_hostname(SSH): 30 | output = SSH.send_command("show run") 31 | dev_name = SSH.send_command("show run | in hostname") 32 | dev_name = dev_name.split(" ") 33 | hostname = dev_name[1] 34 | print("The device name is: ", hostname) 35 | device_commands(hostname, SSH) 36 | 37 | # Push the commands stored into the "Commands.txt" file and store the output in an unique file called with the actual date 38 | def device_commands(device_name, device_SSH): 39 | txt_file = open(dt_string_full + ".txt", "a") 40 | txt_file.write("#####" + device_name + "#####" + device_name + "#####" + device_name + "#####" + device_name + "#####" + device_name + "\n") 41 | for command in commands_list: 42 | txt_file.write("######## " + command + " ######## \n\n") 43 | output = (device_SSH.send_command(command)) 44 | txt_file.write(output + "\n\n") 45 | device_SSH.disconnect() 46 | SSH_Check_Connection = device_SSH.is_alive() 47 | txt_file.write("\n\n\n\n\n\n\n\n ######################################################################################## \n\n\n\n\n\n\n\n") 48 | print("\n\n\n") 49 | 50 | # Verify if an IP address is reachable. 51 | # If YES: run "device_login" function 52 | # If NO: Create a file called with the IP Address of the device and write inside "This device is not reachable. Please verify its status and connectivity" 53 | def ip_reachability(ip_list): 54 | for ip in ip_list: 55 | print("Testing: " + ip) 56 | ip_reach = ping(ip) 57 | if ip_reach == None: 58 | print("The device with the IP Address: " + ip + " is not reachable. Please verify its status and connectivity\n\n\n") 59 | with open(ip + "_" + dt_string_full + ".txt", "w") as downdevice: 60 | downdevice.write("This device is not reachable. Please verify its status and connectivity") 61 | else: 62 | print ("It's reachable! I'm going to retrieve data from it.") 63 | device_login(ip, username, password) 64 | 65 | def discover_network(): 66 | network_mask = input("Enter the management network (x.x.x.x/y): ") 67 | my_scan = networkscan.Networkscan(network_mask) 68 | my_scan.run() 69 | reachable_ip = [] 70 | print("List of reachable devices:\n" ) 71 | for i in my_scan.list_of_hosts_found: 72 | print(i) 73 | reachable_ip.append(i) 74 | ip_reachability(reachable_ip) 75 | 76 | # User enters device credentials 77 | username = input('Enter your username: ') 78 | password = getpass.getpass('Enter your password: ') 79 | 80 | # Code opens the file where commands are stored (1 per line) 81 | file_commands = open("Commands.txt", "r") 82 | total_commands = file_commands.read() 83 | commands_list = total_commands.splitlines() 84 | 85 | # Create Backup Folder 86 | backup_folder = "BACKUP" 87 | create_folder(backup_folder) 88 | 89 | # Create daily folder inside Backup Folder 90 | now = datetime.now() 91 | dt_string = now.strftime("%m-%d-%Y") 92 | dt_string_full = now.strftime("%m-%d-%Y_%H-%M-%S") 93 | create_folder(dt_string) 94 | 95 | # Code 96 | discover_network() -------------------------------------------------------------------------------- /Old/network_automation/Show_Commands_wPing.py: -------------------------------------------------------------------------------- 1 | # With this script you will launch the commands inside the file called 'Commands.txt' to all the devices inside the file called 'IPAddressList.txt 2 | 3 | from typing import Type 4 | from netmiko import ConnectHandler 5 | import credentials 6 | import os_vendor 7 | import os 8 | from datetime import date, datetime 9 | from ping3 import ping 10 | 11 | total_ips = open("IPAddressList.txt", "r") 12 | ip_list_temp = total_ips.readlines() 13 | # The created list has "\n" in each string. I'll create a new empty list in order to fill it with just the IPs without the "\n" 14 | # ['10.155.233.205\n', '10.155.233.201\n', '10.155.233.202\n', '10.155.233.203\n', '10.155.233.204\n', '10.155.233.206'] 15 | ip_list = [] 16 | for ips in ip_list_temp: 17 | temp_list = ips.split("\n") 18 | ip_list.append(temp_list[0]) 19 | #The list called "ip_list" is a list with only the IP addresses 20 | # ['10.155.233.205', '10.155.233.201', '10.155.233.202', '10.155.233.203', '10.155.233.204', '10.155.233.206'] 21 | 22 | total_commands = open("Commands.txt", "r") 23 | commands_temp = total_commands.readlines() 24 | commands_list = [] 25 | for commands in commands_temp: 26 | command_temp_list = commands.split("\n") 27 | commands_list.append(command_temp_list[0]) 28 | print("With this script you will launch the commands inside the file called 'Commands.txt' to all the devices inside the file called 'IPAddressList.txt\n\n\n") 29 | 30 | # Retrive current time and formats it as: Month, Day, Year, Hour and Minute. 31 | now = datetime.now() 32 | dt_string_full = now.strftime("%m-%d-%Y_%H-%M") 33 | dt_string = now.strftime("%m-%d-%Y") 34 | 35 | # If you want to change directory, plese add here the specific folder: 36 | # os.chdir("c:/Users/xxxxx-xxxx") 37 | 38 | # Checks if the backup folder exists, if not, it creates it. Put the name 39 | backup_folder = "CFG_Backup" 40 | if not os.path.exists(backup_folder): 41 | os.mkdir(backup_folder) 42 | os.chdir(backup_folder) 43 | 44 | # Checks if the daily folder exists, if not, it creates it. 45 | if not os.path.exists(dt_string): 46 | os.mkdir(dt_string) 47 | os.chdir(dt_string) 48 | print("I'm going to test the ICMP reachability for all the IP Addresses\n\n") 49 | 50 | for ip in ip_list: 51 | print("Testing: " + ip) 52 | ip_reach = ping(ip) 53 | if ip_reach == None: 54 | print("The device with the IP Address: " + ip + " is not reachable. Please verify its status and connectivity\n\n\n") 55 | with open(ip + "_" + dt_string_full + ".txt", "w") as downdevice: 56 | downdevice.write("This device is not reachable. Please verify its status and connectivity") 57 | else: 58 | print("The device with the IP Address: " + ip + " is reachable. I'm going to connect via SSH and lunch commands\n") 59 | SSH = ConnectHandler (ip, device_type = os_vendor.type_ios, username = credentials.username, password = credentials.password, fast_cli = False) 60 | dev_name = SSH.send_command("show run | in hostname") 61 | dev_name = dev_name.split(" ") 62 | hostname = dev_name[1] 63 | print ("I'm processing the device called", hostname, "with the following IP Address:", ip) 64 | SSH_Check_Connection = SSH.is_alive() 65 | print("The connection to the device is:" + str(SSH_Check_Connection)) 66 | with open(hostname + "_" + dt_string_full + ".txt", "w") as txt_file: 67 | print("The file called: " + hostname + ".txt has been created into the " + backup_folder + "/" + dt_string + " folder. You will find here all the outputs") 68 | for command in commands_list: 69 | txt_file.write("This is the output for the following command:" + command + "\n\n") 70 | output = (SSH.send_command(command)) 71 | txt_file.write(output + "\n\n\n\n") 72 | SSH.disconnect() 73 | SSH_Check_Connection = SSH.is_alive() 74 | print("The connection to the device is:" + str(SSH_Check_Connection)) 75 | print("\n\n\n") 76 | total_ips.close() 77 | total_commands.close() -------------------------------------------------------------------------------- /test.py: -------------------------------------------------------------------------------- 1 | from netmiko import ConnectHandler 2 | import os 3 | from datetime import date, datetime 4 | from ping3 import ping 5 | import getpass 6 | import click 7 | 8 | class test(): 9 | def __init__(self, 10 | username, 11 | password): 12 | self.username = username 13 | self.password = password 14 | 15 | def create_folder(folder): 16 | if not os.path.exists(folder): 17 | os.mkdir(folder) 18 | os.chdir(folder) 19 | 20 | # Login to the devices 21 | def device_login(host, username, password): 22 | cisco_ios = { 23 | 'device_type': 'cisco_ios', 24 | 'host': host, 25 | 'username': username, 26 | 'password': password, 27 | } 28 | try: 29 | connection = ConnectHandler(**cisco_ios) 30 | connection.enable() 31 | test.device_hostname(connection) 32 | except Exception as err: 33 | print(f"Oops! {err}") 34 | 35 | # Retrieve the device Hostname 36 | def device_hostname(SSH): 37 | output = SSH.send_command("show run") 38 | dev_name = SSH.send_command("show run | in hostname") 39 | dev_name = dev_name.split(" ") 40 | hostname = dev_name[1] 41 | print("The device name is: ", hostname) 42 | test.device_commands(hostname, SSH) 43 | 44 | # Push the commands stored into the "Commands.txt" file and store the output in different files called with Device hostname and timestamp 45 | def device_commands(device_name, device_SSH): 46 | with open(device_name + "_" + dt_string_full + ".txt", "w") as txt_file: 47 | print("The file called: " + device_name + ".txt has been created into the " + backup_folder + "/" + dt_string + " folder. You will find here all the outputs. I'm going to close the SSH session") 48 | for command in commands_list: 49 | txt_file.write("######## " + command + " ######## \n\n") 50 | output = (device_SSH.send_command(command)) 51 | txt_file.write(output + "\n\n\n\n") 52 | device_SSH.disconnect() 53 | print("\n\n\n") 54 | 55 | # Verify if an IP address is reachable. 56 | # If YES: run "device_login" function 57 | # If NO: Create a file called with the IP Address of the device and write inside "This device is not reachable. Please verify its status and connectivity" 58 | def ip_reachability(self): 59 | for ip in ip_list: 60 | print("Testing: " + ip) 61 | ip_reach = ping(ip) 62 | if ip_reach == None: 63 | print("The device with the IP Address: " + ip + " is not reachable. Please verify its status and connectivity\n\n\n") 64 | with open(ip + "_" + dt_string_full + ".txt", "w") as downdevice: 65 | downdevice.write("This device is not reachable. Please verify its status and connectivity") 66 | else: 67 | print ("It's reachable! I'm going to retrieve data from it.") 68 | test.device_login(ip, self.username, self.password) 69 | 70 | 71 | # User enters device credentials 72 | #username = input('Enter your username: ') 73 | #password = getpass.getpass('Enter your password: ') 74 | 75 | # Code opens the file where IP Addresses are stored (1 per line) 76 | file_ip = open("IPAddressList.txt", "r") 77 | total_ips = file_ip.read() 78 | ip_list = total_ips.splitlines() 79 | 80 | # Code opens the file where commands are stored (1 per line) 81 | file_commands = open("Commands.txt", "r") 82 | total_commands = file_commands.read() 83 | commands_list = total_commands.splitlines() 84 | 85 | # Create Backup Folder 86 | backup_folder = "BACKUP" 87 | test.create_folder(backup_folder) 88 | 89 | # Create daily folder inside Backup Folder 90 | now = datetime.now() 91 | dt_string = now.strftime("%m-%d-%Y") 92 | dt_string_full = now.strftime("%m-%d-%Y_%H-%M-%S") 93 | test.create_folder(dt_string) 94 | 95 | # Code 96 | @click.command() 97 | @click.option('--username', 98 | prompt="AD Username", 99 | help="AD Username", 100 | required=True,envvar="USERNAME") 101 | @click.option('--password', 102 | prompt="AD Password", 103 | help="AD Password", 104 | required=True, hide_input=True,envvar="PASSWORD") 105 | 106 | def cli(username,password): 107 | invoke_class = test(username,password) 108 | invoke_class.ip_reachability() 109 | 110 | if __name__ == "__main__": 111 | cli() 112 | --------------------------------------------------------------------------------