Tasks:
Explanation
Configuration and Verification
Task1# below script is to run the code for telnet ting to devices mentioned into the file named as Device_Inventory in txt format; script will configure the loopbacks – which are manually mentioned.
import getpass
import telnetlib
user = input(“Enter your username: “)
password = getpass.getpass()
Var_file = open (‘Device_Inventory.txt’)
for 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’) + 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# Below script is to run the code for telnetting to devices mentioned into the file named as Device_Inventory in txt format; script will configure the loopbacks – which are createdby script using nested loops i.e. FOR under FOR.
import getpass
import telnetlib
user = input(“Enter your username: “)
password = getpass.getpass()
Var_file = open (‘Device_Inventory.txt’)
for 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’) + b”\n”)
tn.write(b”conf t\n”)
for VAR_iterate1 in 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# Below script is to run the code for telnetting to devices mentioned into the file named as Device_Inventory in txt format; script will save running configuration into different files with having mentioned IPs in file name.
import getpass
import telnetlib
user = input(“Enter your username: “)
password = getpass.getpass()
Var_file = open (‘Device_Inventory.txt’)
for 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’) + b”\n”)
tn.write(b”terminal length 0\n”)
tn.write(b”show run\n”)
tn.write(b’exit\n’)
readoutput = tn.read_all()
saveoutput = open(“Device_Backup_IP” + IP + “.txt”, “w”)
saveoutput.write(readoutput.decode(‘ascii’))
saveoutput.write(“\n”)
saveoutput.close
Task4# Below script is to run the code for telnetting to devices mentioned into the file named as Device_Inventory in txt format; script will save running configuration, uptime information into different files with having mentioned IPs in file name. Also we are hardcoding username and password – In future we can save this in BAT format.
import getpass
import telnetlib
user = “cisco”
password = “cisco”
Var_file = open (‘Device_Inventory.txt’)
for Var_iterate in Var_file:
IP=Var_iterate.strip()
print (“Entering Device For Uptime ” + (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”show ver | i uptime\n”)
tn.write(b”exit\n”)
readoutput = tn.read_all()
print (“Entering Device For Running Configuration ” + (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”terminal length 0\n”)
tn.write(b”show run\n”)
tn.write(b”exit\n”)
readoutput1 = tn.read_all()
saveoutput = open(“Device_Backup_IP_” + IP + “.txt”, “w”)
saveoutput.write(“**Here is the Device Uptime Information**”)
saveoutput.write(readoutput.decode(‘ascii’))
saveoutput.write(“\n”)
saveoutput.write(“**Here is the Configuration Backup**”)
saveoutput.write(readoutput1.decode(‘ascii’))
saveoutput.write(“\n”)
saveoutput.close
Output is shown as follows –
About the author