Enter username and password to use with Telnet. Loop for range of IP addresses and inside loop again and create new VLAN and name the VLAN. This script connects to two switches using Telnet and adds new configuration.
#!usr/bin/env python
import getpass
import sys
import telnetlib
import time
user = raw_input(“Enter your telnet username: “)
password = getpass.getpass()
print (user, password)
for i in range (1,3):
HOST = ‘192.168.1.’ + str(i)
print (HOST)
tn = telnetlib.Telnet(HOST)
tn.read_until(‘Login: ‘)
tn.write(user + ‘\n’)
if password:
tn.read_until(‘Password: ‘)
tn.write(password + ‘\n’)
time.sleep(5)
tn.write(‘enable’ + ‘\n’)
tn.write(‘conf t’ + ‘\n’)
for v in range (400,404):
print (‘vlan create ‘ + str(v) + ‘ type port-mstprstp 0’ + ‘\n’)
tn.write(‘vlan create ‘ + str(v) + ‘ type port-mstprstp 0’ + ‘\n’)
time.sleep(2)
print (‘vlan name ‘ + str(v) + ‘ VLAN_’ +str(v) + ‘\n’)
tn.write(‘vlan name ‘ + str(v) + ‘ VLAN_’ +str(v) + ‘\n’)
tn.write(‘end’ + ‘\n’)
tn.write(‘exit’ + ‘\n’)
print (“Good bye!”)