3939import time
4040from stub_sftp import StubSFTPServer
4141from tunnel import Tunneler
42-
42+ import gevent . subprocess
4343
4444logger = logging .getLogger ("fake_server" )
4545paramiko_logger = logging .getLogger ('paramiko.transport' )
4646
4747host_key = paramiko .RSAKey (filename = os .path .sep .join ([os .path .dirname (__file__ ), 'rsa.key' ]))
4848
4949class Server (paramiko .ServerInterface ):
50- def __init__ (self , transport , cmd_req_response = {}, fail_auth = False ,
50+ def __init__ (self , transport , fail_auth = False ,
5151 ssh_exception = False ):
5252 self .event = Event ()
53- self .cmd_req_response = cmd_req_response
5453 self .fail_auth = fail_auth
5554 self .ssh_exception = ssh_exception
5655 self .transport = transport
@@ -103,29 +102,31 @@ def check_channel_forward_agent_request(self, channel):
103102 def check_channel_exec_request (self , channel , cmd ):
104103 logger .debug ("Got exec request on channel %s for cmd %s" % (channel , cmd ,))
105104 # Remove any 'bash -c' and/or quotes from command
106- cmd = cmd .replace ('bash -c ' , "" )
107- cmd = cmd .replace ('\" ' , "" )
108- cmd = cmd .replace ('\' ' , "" )
109- if not cmd in self .cmd_req_response :
110- return False
105+ # cmd = cmd.replace('bash -c ', "")
106+ # cmd = cmd.replace('\"', "")
107+ # cmd = cmd.replace('\'', "")
108+ # if not cmd in self.cmd_req_response:
109+ # return False
111110 self .event .set ()
111+ process = gevent .subprocess .Popen (cmd , stdout = gevent .subprocess .PIPE , shell = True )
112112 # Check if response is an iterator in which case we
113113 # do not return but read from iterator and send responses.
114114 # This is to simulate a long running command that has not
115115 # finished executing yet.
116- if hasattr (self .cmd_req_response [cmd ], 'next' ):
117- gevent .spawn (self ._long_running_response ,
118- channel , self .cmd_req_response [cmd ])
119- else :
120- channel .send (self .cmd_req_response [cmd ] + os .linesep )
121- channel .send_exit_status (0 )
116+ gevent .spawn (self ._read_response , channel , process )
117+ # gevent.sleep(1)
118+ # else:
119+ # channel.send(self.cmd_req_response[cmd] + os.linesep)
120+ # channel.send_exit_status(0)
122121 return True
123122
124- def _long_running_response (self , channel , responder ):
125- for response in responder :
126- channel .send (response + os .linesep )
123+ def _read_response (self , channel , process ):
124+ # import ipdb; ipdb.set_trace()
125+ for line in process .stdout :
126+ channel .send (line )
127127 gevent .sleep (0 )
128- channel .send_exit_status (0 )
128+ process .communicate ()
129+ channel .send_exit_status (process .returncode )
129130 channel .close ()
130131
131132def make_socket (listen_ip , port = 0 ):
@@ -140,7 +141,7 @@ def make_socket(listen_ip, port=0):
140141 return
141142 return sock
142143
143- def listen (cmd_req_response , sock , fail_auth = False , ssh_exception = False ,
144+ def listen (sock , fail_auth = False , ssh_exception = False ,
144145 timeout = None ):
145146 """Run a fake ssh server and given a cmd_to_run, send given \
146147 response to client connection. Returns (server, socket) tuple \
@@ -157,18 +158,18 @@ def listen(cmd_req_response, sock, fail_auth=False, ssh_exception=False,
157158 logger .error ('*** Listen failed: %s' % (str (e ),))
158159 traceback .print_exc ()
159160 return
160- handle_ssh_connection (cmd_req_response , sock , fail_auth = fail_auth ,
161+ handle_ssh_connection (sock , fail_auth = fail_auth ,
161162 timeout = timeout , ssh_exception = ssh_exception )
162163
163- def _handle_ssh_connection (cmd_req_response , transport , fail_auth = False ,
164+ def _handle_ssh_connection (transport , fail_auth = False ,
164165 ssh_exception = False ):
165166 try :
166167 transport .load_server_moduli ()
167168 except :
168169 return
169170 transport .add_server_key (host_key )
170171 transport .set_subsystem_handler ('sftp' , paramiko .SFTPServer , StubSFTPServer )
171- server = Server (transport , cmd_req_response = cmd_req_response ,
172+ server = Server (transport ,
172173 fail_auth = fail_auth , ssh_exception = ssh_exception )
173174 try :
174175 transport .start_server (server = server )
@@ -189,7 +190,7 @@ def _handle_ssh_connection(cmd_req_response, transport, fail_auth=False,
189190 gevent .sleep (.2 )
190191 channel .close ()
191192
192- def handle_ssh_connection (cmd_req_response , sock ,
193+ def handle_ssh_connection (sock ,
193194 fail_auth = False , ssh_exception = False ,
194195 timeout = None ):
195196 conn , addr = sock .accept ()
@@ -200,7 +201,7 @@ def handle_ssh_connection(cmd_req_response, sock,
200201 gevent .Timeout (timeout ).start ()
201202 try :
202203 transport = paramiko .Transport (conn )
203- _handle_ssh_connection (cmd_req_response , transport , fail_auth = fail_auth ,
204+ _handle_ssh_connection (transport , fail_auth = fail_auth ,
204205 ssh_exception = ssh_exception )
205206 except Exception , e :
206207 logger .error ('*** Caught exception: %s: %s' % (str (e .__class__ ), str (e ),))
@@ -211,16 +212,16 @@ def handle_ssh_connection(cmd_req_response, sock,
211212 pass
212213 return
213214
214- def start_server (cmd_req_response , sock , fail_auth = False , ssh_exception = False ,
215+ def start_server (sock , fail_auth = False , ssh_exception = False ,
215216 timeout = None ):
216- return gevent .spawn (listen , cmd_req_response , sock , fail_auth = fail_auth ,
217+ return gevent .spawn (listen , sock , fail_auth = fail_auth ,
217218 timeout = timeout , ssh_exception = ssh_exception )
218219
219220if __name__ == "__main__" :
220221 logging .basicConfig ()
221222 logger .setLevel (logging .DEBUG )
222223 sock = make_socket ('127.0.0.1' )
223- server = start_server ({ 'fake' : 'fake response' }, sock )
224+ server = start_server (sock )
224225 try :
225226 server .join ()
226227 except KeyboardInterrupt :
0 commit comments