Commit 65f660a4 authored by ibuler's avatar ibuler

[Update] Telnet支持自定义成功pattern

parent 7afb1bd6
...@@ -338,6 +338,7 @@ defaults = { ...@@ -338,6 +338,7 @@ defaults = {
'LOG_DIR': os.path.join(root_path, 'data', 'logs'), 'LOG_DIR': os.path.join(root_path, 'data', 'logs'),
'REPLAY_DIR': os.path.join(root_path, 'data', 'replays'), 'REPLAY_DIR': os.path.join(root_path, 'data', 'replays'),
'ASSET_LIST_SORT_BY': 'hostname', # hostname, ip 'ASSET_LIST_SORT_BY': 'hostname', # hostname, ip
'TELNET_REGEX': '',
'PASSWORD_AUTH': True, 'PASSWORD_AUTH': True,
'PUBLIC_KEY_AUTH': True, 'PUBLIC_KEY_AUTH': True,
'SSH_TIMEOUT': 10, 'SSH_TIMEOUT': 10,
......
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# #
import os
import re import re
import socket import socket
import telnetlib import telnetlib
...@@ -143,6 +142,17 @@ class SSHConnection: ...@@ -143,6 +142,17 @@ class SSHConnection:
class TelnetConnection: class TelnetConnection:
incorrect_pattern = re.compile(
r'incorrect|failed|失败|错误', re.I
)
username_pattern = re.compile(
r'login:?\s*$|username:?\s*$|用户名:?\s*$|账\s*号:?\s*$', re.I
)
password_pattern = re.compile(
r'Password:?\s*$|passwd:?\s*$|密\s*码:?\s*$', re.I
)
success_pattern = re.compile(r'Last\s*login|success|成功|#|\$', re.I)
custom_success_pattern = None
def __init__(self, asset, system_user, client): def __init__(self, asset, system_user, client):
self.client = client self.client = client
...@@ -150,18 +160,13 @@ class TelnetConnection: ...@@ -150,18 +160,13 @@ class TelnetConnection:
self.system_user = system_user self.system_user = system_user
self.sock = None self.sock = None
self.sel = selectors.DefaultSelector() self.sel = selectors.DefaultSelector()
self.incorrect_pattern = re.compile( if config.TELNET_REGEX:
r'incorrect|failed|失败|错误', re.I try:
) self.custom_success_pattern = re.compile(
self.username_pattern = re.compile( r'{}'.format(config.TELNET_REGEX), re.I
r'login:?\s*$|username:?\s*$|用户名:?\s*$|账\s*号:?\s*$', re.I )
) except (TypeError, ValueError):
self.password_pattern = re.compile( pass
r'Password:?\s*$|passwd:?\s*$|密\s*码:?\s*$', re.I
)
self.success_pattern = re.compile(
r'Last\s*login|success|成功|#|\$', re.I
)
def get_socket(self): def get_socket(self):
logger.debug('Get telnet server socket. {}'.format(self.client.user)) logger.debug('Get telnet server socket. {}'.format(self.client.user))
...@@ -269,7 +274,10 @@ class TelnetConnection: ...@@ -269,7 +274,10 @@ class TelnetConnection:
logger.debug(b'[Password prompt]: ' + b'>>' + raw_data + b'<<') logger.debug(b'[Password prompt]: ' + b'>>' + raw_data + b'<<')
self.sock.send(self.system_user.password.encode('utf-8') + b'\r\n') self.sock.send(self.system_user.password.encode('utf-8') + b'\r\n')
return None return None
elif self.success_pattern.search(data): elif self.success_pattern.search(data) or \
(self.custom_success_pattern and
self.custom_success_pattern.search(data)):
self.client.send(raw_data)
logger.debug(b'[Login Success prompt]: ' + b'>>' + raw_data + b'<<') logger.debug(b'[Login Success prompt]: ' + b'>>' + raw_data + b'<<')
return True return True
else: else:
......
...@@ -385,6 +385,9 @@ class TelnetServer(BaseServer): ...@@ -385,6 +385,9 @@ class TelnetServer(BaseServer):
""" self.chan: socket object """ """ self.chan: socket object """
return getattr(self.chan, '_closed', False) return getattr(self.chan, '_closed', False)
def resize_pty(self):
pass
class Server(BaseServer): class Server(BaseServer):
""" """
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment