2020-10-30 14:56:43 +01:00
|
|
|
import time
|
|
|
|
|
|
|
|
from telnetlib import Telnet
|
|
|
|
|
2020-12-02 21:59:01 +00:00
|
|
|
|
2020-11-24 08:12:17 +01:00
|
|
|
class telnet:
|
2020-10-31 03:06:25 +01:00
|
|
|
def __init__(self, ip, port, username, password):
|
|
|
|
self.ip = ip
|
|
|
|
self.port = port
|
|
|
|
self.username = username
|
2020-10-31 21:07:30 +01:00
|
|
|
self.password = password
|
2020-12-02 22:45:40 +01:00
|
|
|
|
2020-10-31 03:06:25 +01:00
|
|
|
def connect(self, *username):
|
|
|
|
self.session = Telnet(self.ip, self.port)
|
|
|
|
for x in username:
|
|
|
|
self.username = username[0]
|
|
|
|
|
2020-12-02 21:59:01 +00:00
|
|
|
if b'Username:' in self.session.read_until(b'Username:', timeout=3):
|
2020-10-31 03:06:25 +01:00
|
|
|
time.sleep(0.5)
|
2020-12-02 22:45:40 +01:00
|
|
|
self.session.write(bytes(self.username, 'ascii') + b"\r\n")
|
2020-12-02 21:59:01 +00:00
|
|
|
if b'Password:' in self.session.read_until(b'Password:', timeout=3):
|
2020-10-31 03:06:25 +01:00
|
|
|
time.sleep(0.5)
|
|
|
|
self.session.write(bytes(self.password, 'ascii') + b"\r\n")
|
|
|
|
else:
|
|
|
|
raise ConnectionError('Failed to establish telnet connection.')
|