-
Notifications
You must be signed in to change notification settings - Fork 1
/
telnet_multiple_switches_and_vlan.py
60 lines (47 loc) · 1.68 KB
/
telnet_multiple_switches_and_vlan.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
''' telnet and create vlan on multiple cisco switches using files and python '''
# Author: Anubhavsingh Sawdagur
# Created Date: 11 Febuary 2019
import telnetlib
USER = "anubhav"
PASSWORD = "cisco"
FILE = open("Networking\\switches.txt")
for LINE in FILE:
HOST = str(LINE.strip())
print("Telnet to IP: " + HOST)
TN = telnetlib.Telnet(HOST, timeout=15)
# TN.set_debuglevel(1000)
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"term len 0\n") # S1#
TN.write(b"sh ip int br\n")
TN.write(b"conf t\n") # S1(config)#
for n in range(2, 6):
VLAN = "interface vlan " + str(n)
TN.write(VLAN.encode("ascii") + b"\n") # S1(config-if)#
TN.write(b"exit\n") # S1(config)#
GOTO_VLAN = "vlan " + str(n)
TN.write(GOTO_VLAN.encode("ascii") + b"\n") # S1(config-vlan)#
VLAN_NAME = "name Python_Vlan_" + str(n)
TN.write(VLAN_NAME.encode("ascii") + b"\n")
TN.write(b"exit\n") # S1(config)#
TN.write(b"exit\n") # S1#
# To remove created vlans
# -----------------------
# for n in range(2, 6):
# VLAN = "no interface vlan " + str(n) # S1(config)#
# TN.write(VLAN.encode("ascii") + b"\n")
# GOTO_VLAN = "no vlan " + str(n)
# TN.write(GOTO_VLAN.encode("ascii") + b"\n") # S1(config)#
# TN.write(b"exit\n") # S1#
# -----------------------
TN.write(b"sh vlan br\n")
TN.write(b"wr\n")
TN.write(b"\n")
TN.write(b"exit\n")
OUTPUT = TN.read_all().decode('ascii')
print(OUTPUT)
TN.close()
FILE.close()