-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtelnet-template.py
More file actions
56 lines (43 loc) · 1.69 KB
/
telnet-template.py
File metadata and controls
56 lines (43 loc) · 1.69 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
53
54
55
56
#Open telnet connection to devices
def open_telnet_conn(ip):
#Change exception message
try:
#Define telnet parameters
username = 'teopy'
password = 'python'
TELNET_PORT = 23
TELNET_TIMEOUT = 5
READ_TIMEOUT = 5
#Logging into device
connection = telnetlib.Telnet(ip, TELNET_PORT, TELNET_TIMEOUT)
output = connection.read_until("name:", READ_TIMEOUT)
connection.write(username + "\n")
output = connection.read_until("word:", READ_TIMEOUT)
connection.write(password + "\n")
time.sleep(1)
#Setting terminal length for entire output - no 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
#output = connection.read_very_eager()
#print output
#Closing the connection
connection.close()
except IOError:
print "Input parameter error! Please check username, password and file name."
#Calling the Telnet function
open_telnet_conn(ip)