Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
C
coco
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
ops
coco
Commits
4b14b524
Unverified
Commit
4b14b524
authored
Feb 20, 2019
by
老广
Committed by
GitHub
Feb 20, 2019
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #186 from jumpserver/dev
[Update] Telnet支持自定义成功pattern
parents
529e5105
65f660a4
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
26 additions
and
14 deletions
+26
-14
conf.py
coco/conf.py
+1
-0
connection.py
coco/connection.py
+22
-14
models.py
coco/models.py
+3
-0
No files found.
coco/conf.py
View file @
4b14b524
...
@@ -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
,
...
...
coco/connection.py
View file @
4b14b524
# -*- 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
:
...
...
coco/models.py
View file @
4b14b524
...
@@ -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
):
"""
"""
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment