-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtelnet-config.py
203 lines (151 loc) · 5.9 KB
/
telnet-config.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#!/usr/bin/env python
import telnetlib
import threading
import os.path
import subprocess
import time
import sys
#Checking IP address validity
def ip_is_valid():
check = False
global ip_list
while True:
#Prompting user for input
ip_file = raw_input("Enter IP file name and extension: ")
#Changing exception message
try:
#Open user selected file for reading (IP addresses file)
selected_ip_file = open(ip_file, 'r')
#Starting from the beginning of the file
selected_ip_file.seek(0)
#Reading each line (IP address) in the file
ip_list = selected_ip_file.readlines()
#Closing the file
selected_ip_file.close()
except IOError:
print "\nFile %s does not exist! Please check and try again!\n" % ip_file
#Checking octets
for ip in ip_list:
a = ip.split('.')
if (len(a) == 4) and (1 <= int(a[0]) <= 223) and (int(a[0]) != 127) and (int(a[0]) != 169 or int(a[1]) != 254) and (0 <= int(a[1]) <= 255 and 0 <= int(a[2]) <= 255 and 0 <= int(a[3]) <= 255):
check = True
break
else:
print '\n* There was an INVALID IP address! Please check and try again!\n'
check = False
continue
#Evaluating the 'check' flag
if check == False:
continue
elif check == True:
break
#Checking IP reachability
print "\nChecking IP reachability...\n"
check2 = False
while True:
for ip in ip_list:
ping_reply = subprocess.call(['ping', '-c', '3', '-w', '3', '-q', '-n', ip])
if ping_reply == 0:
check2 = True
continue
elif ping_reply == 2:
print "\nNo response from device %s." % ip
check2 = False
break
else:
print "\nPing to the following device has FAILED:", ip
check2 = False
break
#Evaluating the 'check' flag
if check2 == False:
print "Please re-check IP address list or device.\n"
ip_is_valid()
elif check2 == True:
print '\nAll devices are reachable. Waiting for command file...\n'
break
#Checking command file validity
def cmd_is_valid():
global cmd_file
while True:
cmd_file = raw_input("Enter command file name and extension: ")
#Changing exception message
if os.path.isfile(cmd_file) == True:
print "\nSending command(s) to device(s)...\n"
break
else:
print "\nFile %s does not exist! Please check and try again!\n" % cmd_file
continue
#Change exception message
try:
#Calling IP validity function
ip_is_valid()
except KeyboardInterrupt:
print "\n\nProgram aborted by user. Exiting...\n"
sys.exit()
#Change exception message
try:
#Calling command file validity function
cmd_is_valid()
except KeyboardInterrupt:
print "\n\nProgram aborted by user. Exiting...\n"
sys.exit()
#Open telnet connection to devices
def open_telnet_conn(ip):
#Change exception message
try:
#Define telnet parameters
username = 'teopy'
password = 'python'
#Specify the Telnet port (default is 23, anyway)
port = 23
#Specify the connection timeout in seconds for blocking operations, like the connection attempt
connection_timeout = 5
#Specify a timeout in seconds. Read until the string is found or until the timout has passed
reading_timeout = 5
#Logging into device
connection = telnetlib.Telnet(ip, port, connection_timeout)
#Waiting to be asked for an username
router_output = connection.read_until("Username:", reading_timeout)
#Enter the username when asked and a "\n" for Enter
connection.write(username + "\n")
#Waiting to be asked for a password
router_output = connection.read_until("Password:", reading_timeout)
#Enter the password when asked and a "\n" for Enter
connection.write(password + "\n")
time.sleep(1)
#Setting terminal length for the entire output - disabling pagination
connection.write("terminal length 0\n")
time.sleep(1)
#Entering global config mode
connection.write("\n")
connection.write("configure terminal\n")
time.sleep(1)
#Open user selected file for reading
selected_cmd_file = open(cmd_file, 'r')
#Starting from the beginning of the file
selected_cmd_file.seek(0)
#Writing each line in the file to the device
for each_line in selected_cmd_file.readlines():
connection.write(each_line + '\n')
time.sleep(1)
#Closing the file
selected_cmd_file.close()
#Test for reading command output
#router_output = connection.read_very_eager()
#print router_output
#Closing the connection
connection.close()
except IOError:
print "Input parameter error! Please check username, password and file name."
#Creating threads
def create_threads():
threads = []
for ip in ip_list:
th = threading.Thread(target = open_telnet_conn, args = (ip,)) #args is a tuple with a single element
th.start()
threads.append(th)
for th in threads:
th.join()
#Calling threads creation function
create_threads()
#End of program