Tasks:
Create a script to open a file containing the device’s inventory. Add loopback 10,11,12 to them.
You can configure 50 loopbacks for all devices, starting at 1 to 50, and assign ip 50.1.1.x/32. x is the number of devices.
To backup all devices for running configuration in different files.
To obtain uptime and device backup, you don’t need to create username/password.
Explanation
While we won’t hard-cord devices IPs at this time, we will make a script to receive them from an inventory file stored externally in txt.
These IPs can be accessed to execute desired commands.
Backup files will be saved as and when needed.
Configuration and verification
Task1# below script is for running the code to telnet to devices mentioned in the Device_Inventory file in txt format. The script will configure loopbacks – which can be manually mentioned.
import getpass
import telnetlib
user = input (“Enter your username here:”)
password = getpass.getpass()
Var_file = open (‘Device_Inventory.txt’)
Var_file:
IP=VAR_iterate.strip()
Print (“Configuring Switch” + (IP).
tn = telnetlib.Telnet(IP)
tn.read_until(b”Username: “)
tn.write(user.encode(‘ascii’) + b”\n”)
if password:
tn.read_until(b”Password: “)
tn.write(password.encode(‘ascii’) + b”\n”)
tn.write(b”conf t\n”)
tn.write(b”int loop 10\n”)
tn.write(b”description Python_Loop_10\n”)
tn.write(b”exit\n”)
tn.write(b”int loop 11\n”)
tn.write(b”description Python_Loop_11\n”)
tn.write(b”exit\n”)
tn.write(b”int loop 12\n”)
tn.write(b”description Python_Loop_12\n”)
tn.write(b”exit\n”)
tn.write(b”end\n”)
tn.write(b”exit\n”)
print(tn.read_all().decode(‘ascii’))
Task2# The script below will run the code to telnet to devices listed in the Device_Inventory file in txt format. The script will then configure loopbacks, which are created by script using nested loops. FOR under FOR.
import getpass
import telnetlib
user = input (“Enter your username here:”)
password = getpass.getpass()
Var_file = open (‘Device_Inventory.txt’)
Var_file:
IP=VAR_iterate.strip()
Print (“Configuring Switch” + (IP).
tn = telnetlib.Telnet(IP)
tn.read_until(b”Username: “)
tn.write(user.encode(‘ascii’) + b”\n”)
if password:
tn.read_until(b”Password: “)
tn.write(password.encode(‘ascii’) + b”\n”)
tn.write(b”conf t\n”)
For VAR_iterate1 range (1,52),:
tn.write(b”int loop ” + str(VAR_iterate1).encode(‘ascii’) + b”\n”)
tn.write(b”description Python_Loopback_” + str(VAR_iterate1).encode(‘ascii’) + b”\n”)
tn.write(b”ip address 50.1.1.” + str(VAR_iterate1).encode(‘ascii’) + b” 255.255.255.255” + b”\n”)
tn.write(b”end\n”)
tn.write(b”wr\n”)
tn.write(b”exit\n”)
print(tn.read_all().decode(‘ascii’))
Task3# This script will run the code to telnet to devices named Device_Inventory in the txt file. The script will save configurations to other files that have the IPs mentioned in the file name.
import getpass
import telnetlib
user = input (“Enter your username here:”)
password = getpass.getpass()
Var_file = open (‘Device_Inventory.txt’)
VAR_iterate in Var_file
IP=VAR_iterate.strip()
Print (“Configuring Switch” + (IP).
tn = telnetlib.Telnet(IP)
tn.read_until(b”Username: “)
tn.write(user.encode(‘ascii’) + b”\n”)
if password:
tn.read_until(b”Password: “)
tn.write(password.encode(‘ascii’)