|
| 1 | +# Automating Network Management with Python: Telnet Scripts for Network Engineers |
| 2 | + |
| 3 | +In today's fast-paced network environments, automation is key to efficiency. Python, with its powerful libraries, allows network engineers to automate repetitive tasks. This blog post showcases several Telnet scripts that demonstrate how to manage network devices effectively. We will cover scripts for backing up configurations, creating VLANs, and executing commands on multiple switches. |
| 4 | + |
| 5 | +## Prerequisites |
| 6 | + |
| 7 | +Before you dive into the scripts, ensure you have Python installed along with the `telnetlib` and `getpass` modules. These libraries are part of Python’s standard library, so you won't need to install anything extra. |
| 8 | + |
| 9 | +## Exercise 1: Simple Command Execution |
| 10 | + |
| 11 | +The first exercise demonstrates how to execute a command on a network device via Telnet. This script prompts for a username, password, and command, establishing a connection to the specified device. |
| 12 | + |
| 13 | +```python |
| 14 | +import getpass |
| 15 | +import telnetlib |
| 16 | + |
| 17 | +HOST = "172.16.141.11" |
| 18 | +user = input("Enter your Username: ") |
| 19 | +password = getpass.getpass() |
| 20 | + |
| 21 | +tn = telnetlib.Telnet(HOST) |
| 22 | +tn.read_until(b"Username: ") |
| 23 | +tn.write(user.encode("ascii") + b"\n") |
| 24 | +if password: |
| 25 | + tn.read_until(b"Password: ") |
| 26 | + tn.write(password.encode("ascii") + b"\n") |
| 27 | + |
| 28 | +tn.write(b"sh ip int brief\n") |
| 29 | +tn.write(b"exit\n") |
| 30 | + |
| 31 | +print(tn.read_all().decode("ascii")) |
| 32 | +``` |
| 33 | + |
| 34 | +### Key Features: |
| 35 | + |
| 36 | +- Prompts for user credentials. |
| 37 | +- Executes the command `show ip interface brief`. |
| 38 | + |
| 39 | +## Exercise 2: Backup Running Configuration |
| 40 | + |
| 41 | +This script allows you to retrieve the running configuration from a device. The script requests user credentials and saves the device's configuration to a file. |
| 42 | + |
| 43 | +```python |
| 44 | +import getpass |
| 45 | +import telnetlib |
| 46 | + |
| 47 | +IP = input("Enter the IP Address: ") |
| 48 | +user = input("Enter your username: ") |
| 49 | +password = getpass.getpass() |
| 50 | +tn = telnetlib.Telnet(IP) |
| 51 | +tn.read_until(b"Username: ") |
| 52 | +tn.write(user.encode("ascii") + b"\n") |
| 53 | +if password: |
| 54 | + tn.read_until(b"Password: ") |
| 55 | + tn.write(password.encode("ascii") + b"\n") |
| 56 | +tn.write(b"enable\n") |
| 57 | +tn.write(b"cisco\n") |
| 58 | +tn.write(b"terminal length 0\n") |
| 59 | +tn.write(b"show run\n") |
| 60 | +tn.write(b"exit\n") |
| 61 | +print(tn.read_all().decode("ascii")) |
| 62 | +``` |
| 63 | + |
| 64 | +### Key Features: |
| 65 | + |
| 66 | +- Uses `enable` mode for privileged commands. |
| 67 | +- Retrieves the running configuration and displays it. |
| 68 | + |
| 69 | +## Exercise 3: Create Multiple VLANs on Multiple Switches |
| 70 | + |
| 71 | +In this exercise, we automate the creation of multiple VLANs across several switches. A file containing the IP addresses of the switches is required. |
| 72 | + |
| 73 | +```python |
| 74 | +import getpass |
| 75 | +import telnetlib |
| 76 | + |
| 77 | +user = input("Enter your username: ") |
| 78 | +password = getpass.getpass() |
| 79 | + |
| 80 | +f = open("switch.cfg") |
| 81 | + |
| 82 | +for IP in f: |
| 83 | + IP = IP.strip() |
| 84 | + print("Configuring Switch " + IP) |
| 85 | + tn = telnetlib.Telnet(IP) |
| 86 | + tn.read_until(b"Username: ") |
| 87 | + tn.write(user.encode("ascii") + b"\n") |
| 88 | + if password: |
| 89 | + tn.read_until(b"Password: ") |
| 90 | + tn.write(password.encode("ascii") + b"\n") |
| 91 | + tn.write(b"enable\n") |
| 92 | + tn.write(b"cisco\n") |
| 93 | + tn.write(b"conf t\n") |
| 94 | + |
| 95 | + for n in range(2, 10): |
| 96 | + tn.write(b"vlan " + str(n).encode("ascii") + b"\n") |
| 97 | + tn.write(b"name VLAN_" + str(n).encode("ascii") + b"\n") |
| 98 | + tn.write(b"end\n") |
| 99 | + tn.write(b"show vlan br\n\n") |
| 100 | + tn.write(b"exit\n") |
| 101 | + print(tn.read_all().decode("ascii")) |
| 102 | +``` |
| 103 | + |
| 104 | +### Key Features: |
| 105 | + |
| 106 | +- Reads a list of switch IPs from a file. |
| 107 | +- Configures VLANs 2 through 9 on each switch. |
| 108 | + |
| 109 | +## Exercise 4: Backup Configuration of All Switches |
| 110 | + |
| 111 | +This script backs up the configuration of multiple switches by reading their IPs from a file and saving the output to individual files. |
| 112 | + |
| 113 | +```python |
| 114 | +import getpass |
| 115 | +import telnetlib |
| 116 | + |
| 117 | +user = input("Enter your username: ") |
| 118 | +password = getpass.getpass() |
| 119 | +f = open("device_ip.cfg") |
| 120 | +for IP in f: |
| 121 | + IP = IP.strip() |
| 122 | + print("Taking backup of device " + IP) |
| 123 | + tn = telnetlib.Telnet(IP) |
| 124 | + tn.read_until(b"Username: ") |
| 125 | + tn.write(user.encode("ascii") + b"\n") |
| 126 | + if password: |
| 127 | + tn.read_until(b"Password: ") |
| 128 | + tn.write(password.encode("ascii") + b"\n") |
| 129 | + tn.write(b"terminal length 0\n") |
| 130 | + tn.write(b"show run\n") |
| 131 | + tn.write(b"exit\n") |
| 132 | + |
| 133 | + output = tn.read_all() |
| 134 | + config = open("device_" + IP, "w") |
| 135 | + config.write(output.decode("ascii")) |
| 136 | + config.write("\n") |
| 137 | + config.close() |
| 138 | + print(tn.read_all().decode("ascii")) |
| 139 | +``` |
| 140 | + |
| 141 | +### Key Features: |
| 142 | + |
| 143 | +- Reads device IPs from a file. |
| 144 | +- Saves each switch's running configuration in separate files. |
| 145 | + |
| 146 | +## Conclusion |
| 147 | + |
| 148 | +These Python scripts provide a solid foundation for automating network management tasks via Telnet. By leveraging these tools, network engineers can streamline their workflows, reduce human error, and save valuable time. Remember to modify and adapt the scripts according to your network environment and security practices. |
0 commit comments