Python – Script5: Configure Multiple Devices via separate file

Configure Vlans 2-9 on devices 192.168.10.21 – 192.168.10.24 via separate file & save Configs

Step1: Configure file with all IP details of multiple devices

nano myswitches

192.168.10.21
192.168.10.22
192.168.10.23
192.168.10.24

Step2: Configure file with for python script

nano switchloopfile.py

import getpass
import sys
import telnetlib
 
user = raw_input(“Enter your telnet username: “)
password = getpass.getpass()
 
f = open (‘myswitches’)
for line in f:
    print “Telnet to host ” + (line)
    HOST = line.strip()
    tn = telnetlib.Telnet(HOST)
 
    tn.read_until(“Username: “)
    tn.write(user + “\n”)
    if password:
              tn.read_until(“Password: “)
              tn.write(password + “\n”)
 
    tn.write(“enable\n”)
    tn.write(“cisco\n”)
    tn.write(“conf t\n”)
 
    for n in range(2,10):
        tn.write(“Vlan ” + str(n) + “\n”)
        tn.write(“name Python_VLAN_” + str(n) + “\n”)
 
    tn.write(“end\n”)
    tn.write(“wr\n”)
    tn.write(“exit\n”)
 
    print tn.read_all()

Network Architect | CCIEx3 #29824 JNCIE #2197 VCIX-NV

Leave a Comment