-
Notifications
You must be signed in to change notification settings - Fork 0
/
ext2_telnetlib_v2.py
executable file
·81 lines (73 loc) · 2.11 KB
/
ext2_telnetlib_v2.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
#!/usr/bin/env python
'''
Write a script that connects to the lab pynet-rtr1, logins, and executes the
'show ip int brief' command.
'''
import telnetlib
import time
import socket
import sys
import getpass
TELNET_PORT = 23
TELNET_TIMEOUT = 6
class CiscoDevice(object):
def __init__(self, ip_addr, uname):
self.ip = ip_addr
self.uname = uname
self.remote_conn = None
def telnet_connect(self):
'''
Establish telnet connection
'''
try:
self.remote_conn = telnetlib.Telnet(self.ip, TELNET_PORT, TELNET_TIMEOUT)
return self.remote_conn
except socket.timeout:
sys.exit("Connection timed-out")
def login(self, passw):
'''
Login to network device
'''
output = self.remote_conn.read_until("sername:", TELNET_TIMEOUT)
self.remote_conn.write(self.uname + '\n')
output += self.remote_conn.read_until("ssword:", TELNET_TIMEOUT)
self.remote_conn.write(passw + '\n')
return output
def send_command(self, cmd):
'''
Send a command down the telnet channel
Return the response
'''
cmd = cmd.rstrip()
self.remote_conn.write(cmd + '\n')
time.sleep(1)
return self.remote_conn.read_very_eager()
def disable_paging(self, paging_cmd='terminal length 0'):
'''
Disable the paging of output (i.e. --More--)
'''
return self.send_command(paging_cmd)
def disconnect(self):
self.remote_conn.close()
def main():
'''
Write a script that connects to the lab pynet-rtr1, logins, and executes the
'show ip int brief' command.
'''
ip_addr = raw_input("IP address: ")
ip_addr = ip_addr.strip()
username = 'pyclass'
password = getpass.getpass()
rtr = CiscoDevice(ip_addr, username)
rtr.telnet_connect()
time.sleep(1)
rtr.login(password)
time.sleep(1)
rtr.disable_paging()
output = rtr.send_command("show ip int brief")
print "\n\n"
print output
print "\n\n"
rtr.disconnect()
if __name__ == "__main__":
main()