Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
J
jumpserver
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
jumpserver
Commits
f946a4bf
Commit
f946a4bf
authored
Sep 22, 2016
by
ibuler
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
finish example
parent
db2d00f8
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
51 additions
and
30 deletions
+51
-30
server.py
server.py
+51
-30
No files found.
server.py
View file @
f946a4bf
...
...
@@ -6,10 +6,13 @@
import
base64
from
binascii
import
hexlify
import
os
import
socket
import
sys
import
threading
import
traceback
import
tty
import
termios
import
struct
,
fcntl
,
signal
,
socket
,
select
import
errno
import
paramiko
from
paramiko.py3compat
import
b
,
u
,
decodebytes
...
...
@@ -20,7 +23,7 @@ paramiko.util.log_to_file('demo_server.log')
host_key
=
paramiko
.
RSAKey
(
filename
=
'test_rsa.key'
)
class
S
erver
(
paramiko
.
ServerInterface
):
class
S
SHService
(
paramiko
.
ServerInterface
):
# 'data' is the output of base64.encodestring(str(key))
# (using the "user_rsa_key" files)
data
=
(
b
'AAAAB3NzaC1yc2EAAAABIwAAAIEAyO4it3fHlmGZWJaGrfeHOVY7RWO3P9M7hp'
...
...
@@ -68,11 +71,19 @@ class SSHServer:
self
.
sock
=
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_STREAM
)
self
.
sock
.
setsockopt
(
socket
.
SOL_SOCKET
,
socket
.
SO_REUSEADDR
,
1
)
self
.
sock
.
bind
((
self
.
host
,
self
.
port
))
@staticmethod
def
handle_ssh_request
(
client
,
addr
):
self
.
server_ssh
=
None
self
.
server_chan
=
None
def
connect
(
self
):
ssh
=
paramiko
.
SSHClient
()
ssh
.
set_missing_host_key_policy
(
paramiko
.
AutoAddPolicy
())
ssh
.
connect
(
hostname
=
'127.0.0.1'
,
port
=
22
,
username
=
'root'
,
password
=
'redhat'
)
self
.
server_ssh
=
ssh
self
.
server_chan
=
channel
=
ssh
.
invoke_shell
(
term
=
'xterm'
)
return
channel
def
handle_ssh_request
(
self
,
client
,
addr
):
print
(
'Got a connection!'
)
try
:
t
=
paramiko
.
Transport
(
client
,
gss_kex
=
False
)
t
.
set_gss_host
(
socket
.
getfqdn
(
""
))
...
...
@@ -82,35 +93,45 @@ class SSHServer:
print
(
'(Failed to load moduli -- gex will be unsupported.)'
)
raise
t
.
add_server_key
(
host_key
)
server
=
Server
()
server
.
add_prompt
(
">>"
)
service
=
SSHService
()
try
:
t
.
start_server
(
server
=
serv
er
)
t
.
start_server
(
server
=
serv
ice
)
except
paramiko
.
SSHException
:
print
(
'*** SSH negotiation failed.'
)
return
chan
=
t
.
accept
(
20
)
if
chan
is
None
:
print
(
'*** No channel.'
)
return
print
(
'Authenticated!'
)
chan
.
settimeout
(
100
)
chan
.
send
(
'
\r\n\r\n
Welcome to my dorky little BBS!
\r\n\r\n
'
)
chan
.
send
(
'We are on fire all the time! Hooray! Candy corn for everyone!
\r\n
'
)
chan
.
send
(
'Happy birthday to Robot Dave!
\r\n\r\n
'
)
server_chan
=
self
.
connect
()
if
not
service
.
event
.
is_set
():
print
(
'*** Client never asked for a shell.'
)
return
while
True
:
# wait for auth
chan
=
t
.
accept
(
20
)
if
chan
is
None
:
print
(
'*** No channel.'
)
return
print
(
'Authenticated!'
)
server
.
event
.
wait
(
10
)
if
not
server
.
event
.
is_set
():
print
(
'*** Client never asked for a shell.'
)
return
chan
.
send
(
'
\r\n\r\n
Welcome to my dorky little BBS!
\r\n\r\n
'
)
chan
.
send
(
'We are on fire all the time! Hooray! Candy corn for everyone!
\r\n
'
)
chan
.
send
(
'Happy birthday to Robot Dave!
\r\n\r\n
'
)
chan
.
send
(
'Username: '
)
f
=
chan
.
makefile
(
'rU'
)
username
=
f
.
readline
()
.
strip
(
'
\r\n
'
)
chan
.
send
(
'
\r\n
I don
\'
t like you, '
+
username
+
'.
\r\n
'
)
chan
.
close
()
r
,
w
,
e
=
select
.
select
([
server_chan
,
chan
],
[],
[])
if
chan
in
r
:
recv_data
=
chan
.
recv
(
1024
)
.
decode
(
'utf8'
)
print
(
"From client: "
+
repr
(
recv_data
))
if
len
(
recv_data
)
==
0
:
break
server_chan
.
send
(
recv_data
)
if
server_chan
in
r
:
recv_data
=
server_chan
.
recv
(
1024
)
.
decode
(
'utf8'
)
print
(
"From server: "
+
repr
(
recv_data
))
if
len
(
recv_data
)
==
0
:
break
chan
.
send
(
recv_data
)
except
Exception
as
e
:
print
(
'*** Caught exception: '
+
str
(
e
.
__class__
)
+
': '
+
str
(
e
))
...
...
@@ -127,7 +148,7 @@ class SSHServer:
try
:
client
,
addr
=
self
.
sock
.
accept
()
print
(
'Listening for connection ...'
)
threading
.
Thread
(
target
=
self
.
handle_ssh_request
,
args
=
(
client
,
addr
))
.
start
()
threading
.
Thread
(
target
=
self
.
handle_ssh_request
,
args
=
(
client
,
addr
))
.
start
()
except
Exception
as
e
:
print
(
'*** Bind failed: '
+
str
(
e
))
traceback
.
print_exc
()
...
...
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