-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtelnetExampleSwitchLoop.py
More file actions
52 lines (42 loc) · 1.48 KB
/
telnetExampleSwitchLoop.py
File metadata and controls
52 lines (42 loc) · 1.48 KB
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
#! /bin/bash/python3
# Telnetexample connecting to a switch and configuring vlans
import getpass
import telnetlib
import time
import os
ipaddress = input("Enter the IP Address of Device: ") # example
print("I'll attempt to ping: " + ipaddress)
response = os.system("ping -c 3 " + ipaddress)
#and then check the response...
if response == 0:
print(ipaddress, 'is up!')
print("The current time is:", time.asctime())
user = input("Enter your cisco username: ")
password = getpass.getpass()
tn = telnetlib.Telnet(ipaddress)
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 len 0\n")
tn.write(b"configure terminal\n")
for x in range(10, 31): # VLAN10 -> VLAN30
tn.write("vlan {}\n".format(x).encode())
tn.write("name VLAN{}\n".format(x).encode())
tn.write(b"exit\n")
tn.write(b"exit\n")
tn.write(b"write memory\n")
tn.write(b"exit\n")
tn.write(b"exit\n")
str_object = ("VLAN{} was added to the switch".format(x).encode())
#str_object = str_object.decode()
print(str_object.decode())
print(tn.read_all().decode('ascii'))
print("The loop ended here")
else:
print(ipaddress, 'is down!')
print("\n")
print("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*")
print("The script ended.")
print("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*")