Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env
- import telnetlib
- import time
- import socket
- import sys
- TELNET_PORT = 23
- TELNET_TIMEOUT = 8
- class Telnet(object):
- def __init__(self, username, password, ip_addr, port):
- self.username = username
- self.password = password
- self.ip_addr = ip_addr
- self.port = port
- self.TIMEOUT = 8
- def login(self, remote_conn):
- output = remote_conn.read_until('sername:', self.TIMEOUT)
- remote_conn.write(self.username + '\n')
- output += remote_conn.read_until('ssword:', self.TIMEOUT)
- remote_conn.write(self.password + '\n')
- return output
- def send_command(self, remote_conn, command):
- command = command.rstrip()
- remote_conn.write(command + '\n')
- time.sleep(1)
- return remote_conn.read_very_eager()
- @property
- def telnet_connect(self):
- try:
- return telnetlib.Telnet(self.ip_addr, self.port, self.TIMEOUT)
- except socket.timeout:
- sys.exit('connection timed-out')
- def main():
- router1 = Telnet('pyclass', '88newclass', '184.105.247.70', 23)
- remote_conn = router1.telnet_connect
- output = router1.login(remote_conn)
- time.sleep(1)
- output = remote_conn.read_very_eager()
- output = router1.send_command(remote_conn, 'terminal length 0')
- output = router1.send_command(remote_conn, 'show version')
- output = output.strip('show version')
- output = output[:output.rfind('\n')]
- print(output)
- remote_conn.close()
- if __name__ == "__main__": main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement