Skip to content

Commit cb3e1a5

Browse files
author
Dan
committed
Merging with server refactoring changes
1 parent f5ea647 commit cb3e1a5

File tree

3 files changed

+49
-24
lines changed

3 files changed

+49
-24
lines changed

embedded_server/embedded_server.py

Lines changed: 45 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
3131
Does _not_ support interactive shells, our clients do not use them.
3232
33-
Server private key is hardcoded. Server listen code inspired by demo_server.py in \
33+
Server private key is hardcoded. Server listen code inspired by demo_server.py in
3434
Paramiko repository.
3535
3636
Server runs asynchronously in its own greenlet. Call `start_server` with a new `multiprocessing.Process` to run it on a new process with its own event loop.
@@ -62,12 +62,34 @@
6262
os.path.dirname(__file__), 'rsa.key']))
6363

6464
class Server(paramiko.ServerInterface):
65-
def __init__(self, transport, fail_auth=False,
65+
"""Implements :mod:`paramiko.ServerInterface` to provide an
66+
embedded SSH server implementation.
67+
68+
Start a `Server` with at least a transport and a host key.
69+
70+
Any SSH2 client with public key or password authentication
71+
is allowed, only. Shell requests are not accepted.
72+
73+
Implemented:
74+
* Direct tcp-ip channels (tunneling)
75+
* SSH Agent forwarding on request
76+
* PTY requests
77+
* Exec requests (run a command on server)
78+
79+
Not Implemented:
80+
* Shell requests
81+
"""
82+
83+
def __init__(self, transport, host_key, fail_auth=False,
6684
ssh_exception=False):
6785
self.event = Event()
6886
self.fail_auth = fail_auth
6987
self.ssh_exception = ssh_exception
7088
self.transport = transport
89+
self.host_key = host_key
90+
transport.load_server_moduli()
91+
transport.add_server_key(self.host_key)
92+
transport.set_subsystem_handler('sftp', paramiko.SFTPServer, StubSFTPServer)
7193

7294
def check_channel_request(self, kind, chanid):
7395
return paramiko.OPEN_SUCCEEDED
@@ -175,33 +197,29 @@ def listen(sock, fail_auth=False, ssh_exception=False,
175197

176198
def _handle_ssh_connection(transport, fail_auth=False,
177199
ssh_exception=False):
178-
try:
179-
transport.load_server_moduli()
180-
except:
181-
return
182-
transport.add_server_key(host_key)
183-
transport.set_subsystem_handler('sftp', paramiko.SFTPServer, StubSFTPServer)
184-
server = Server(transport,
200+
server = Server(transport, HOST_KEY,
185201
fail_auth=fail_auth, ssh_exception=ssh_exception)
202+
# server.run()
186203
try:
187204
transport.start_server(server=server)
188205
except paramiko.SSHException as e:
189206
logger.exception('SSH negotiation failed')
190-
return
191207
except Exception:
192208
logger.exception("Error occured starting server")
193209
return
194-
gevent.sleep(0)
195-
channel = transport.accept(20)
196-
if not channel:
197-
logger.error("Could not establish channel")
198-
return
199-
while transport.is_active():
200-
logger.debug("Transport active, waiting..")
201-
gevent.sleep(1)
202-
while not channel.send_ready():
203-
gevent.sleep(.2)
204-
channel.close()
210+
while True:
211+
gevent.sleep(0)
212+
channel = transport.accept(20)
213+
if not channel:
214+
logger.error("Could not establish channel")
215+
return
216+
while transport.is_active():
217+
logger.debug("Transport active, waiting..")
218+
gevent.sleep(1)
219+
while not channel.send_ready():
220+
gevent.sleep(.2)
221+
channel.close()
222+
gevent.sleep(0)
205223

206224
def handle_ssh_connection(sock,
207225
fail_auth=False, ssh_exception=False,
@@ -227,8 +245,12 @@ def handle_ssh_connection(sock,
227245

228246
def start_server(sock, fail_auth=False, ssh_exception=False,
229247
timeout=None):
230-
return gevent.spawn(listen, sock, fail_auth=fail_auth,
231-
timeout=timeout, ssh_exception=ssh_exception)
248+
g = gevent.spawn(listen, sock, fail_auth=fail_auth,
249+
timeout=timeout, ssh_exception=ssh_exception)
250+
try:
251+
g.join()
252+
except KeyboardInterrupt:
253+
sys.exit(0)
232254

233255
if __name__ == "__main__":
234256
logging.basicConfig()

examples/parallel_commands.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
import datetime
33

44
output = []
5-
host = 'localhost'
5+
host = '192.168.1.2'
66
hosts = [host]
77
client = ParallelSSHClient(hosts)
8+
import ipdb; ipdb.set_trace()
89

910
# Run 10 five second sleeps
1011
cmds = ['sleep 5' for _ in xrange(10)]
@@ -17,5 +18,6 @@
1718
start = datetime.datetime.now()
1819
for _output in output:
1920
client.join(_output)
21+
print(_output)
2022
end = datetime.datetime.now()
2123
print("All commands finished in %s" % (end-start,))

pssh/ssh_client.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ def __init__(self, host,
120120
self.proxy_pkey = proxy_host, proxy_port, proxy_user, \
121121
proxy_password, proxy_pkey
122122
self.proxy_client = None
123+
import ipdb; ipdb.set_trace()
123124
if self.proxy_host and self.proxy_port:
124125
logger.debug("Proxy configured for destination host %s - Proxy host: %s:%s",
125126
self.host, self.proxy_host, self.proxy_port,)

0 commit comments

Comments
 (0)