2022-07-29 02:02:23 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
import socket
|
|
|
|
import telnetlib
|
|
|
|
|
|
|
|
PROMPT_REGEX = r'[\<|\[](.*)[\>|\]]'
|
|
|
|
|
|
|
|
|
|
|
|
def telnet(dest_addr, port_number=23, timeout=10):
|
|
|
|
try:
|
|
|
|
connection = telnetlib.Telnet(dest_addr, port_number, timeout)
|
|
|
|
except (ConnectionRefusedError, socket.timeout, socket.gaierror) as e:
|
|
|
|
return False, str(e)
|
|
|
|
expected_regexes = [bytes(PROMPT_REGEX, encoding='ascii')]
|
|
|
|
index, prompt_regex, output = connection.expect(expected_regexes, timeout=3)
|
2022-09-07 09:37:37 +00:00
|
|
|
return True, output.decode('utf-8', 'ignore')
|
2022-07-29 02:02:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
print(telnet(dest_addr='1.1.1.1', port_number=2222))
|
|
|
|
print(telnet(dest_addr='baidu.com', port_number=80))
|
|
|
|
print(telnet(dest_addr='baidu.com', port_number=8080))
|
|
|
|
print(telnet(dest_addr='192.168.4.1', port_number=2222))
|
|
|
|
print(telnet(dest_addr='192.168.4.1', port_number=2223))
|
|
|
|
print(telnet(dest_addr='ssssss', port_number=-1))
|