-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtelnetlib
51 lines (39 loc) · 1.18 KB
/
telnetlib
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
#! /usr/bin/env python
import telnetlib
import time
import socket
import sys
TELNET_PORT = 23
TELNET_TIMEOUT = 6
def send_command(remote_conn, cmd):
cmd = cmd.rstrip()
remote_conn.write("terminal length 0" + "\n")
time.sleep(1)
return remote_conn.read_very_eager()
def login(remote_conn, username, password):
output = remote_conn.read_until("sername:", TELNET_TIMEOUT)
remote_conn.write(username + '\n')
output += remote_conn.read_until("assword:", TELNET_TIMEOUT)
remote_conn.write(password + '\n')
return output
def telnet_connect(ip_addr):
try:
return telnetlib.Telnet(ip_addr, TELNET_PORT, TELNET_TIMEOUT)
except socket.timeout:
sys.exit("Connection timed-out")
def main():
ip_addr = "10.5.6.7"
username = 'myuser'
password = 'mypwd'
remote_conn = telnet_connect(ip_addr)
ouptut = login(remote_conn, username, password)
time.sleep(1)
output = remote_conn.read_very_eager()
# Disable paging
output = send_command(remote_conn, "terminal length 0")
# Show version
output = send_command(remote_conn, "show version")
remote_conn.close()
# this runs the function if executed directly, instead of by importing from another script
if __name__ == "__main__":
main()