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 , cmd_req_response = {}, fail_auth = False ,
51+ ssh_exception = False ):
5152 self .event = Event ()
5253 self .cmd_req_response = cmd_req_response
5354 self .fail_auth = fail_auth
55+ self .ssh_exception = ssh_exception
5456 self .transport = transport
5557
5658 def check_channel_request (self , kind , chanid ):
5759 return paramiko .OPEN_SUCCEEDED
5860
5961 def check_auth_password (self , username , password ):
60- if self .fail_auth : return paramiko .AUTH_FAILED
62+ if self .fail_auth :
63+ return paramiko .AUTH_FAILED
64+ if self .ssh_exception :
65+ raise paramiko .SSHException ()
6166 return paramiko .AUTH_SUCCESSFUL
6267
6368 def check_auth_publickey (self , username , key ):
64- if self .fail_auth : return paramiko .AUTH_FAILED
69+ if self .fail_auth :
70+ return paramiko .AUTH_FAILED
71+ if self .ssh_exception :
72+ raise paramiko .SSHException ()
6573 return paramiko .AUTH_SUCCESSFUL
6674
6775 def get_allowed_auths (self , username ):
@@ -131,7 +139,7 @@ def make_socket(listen_ip, port=0):
131139 return
132140 return sock
133141
134- def listen (cmd_req_response , sock , fail_auth = False ,
142+ def listen (cmd_req_response , sock , fail_auth = False , ssh_exception = False ,
135143 timeout = None ):
136144 """Run a fake ssh server and given a cmd_to_run, send given \
137145 response to client connection. Returns (server, socket) tuple \
@@ -149,17 +157,18 @@ def listen(cmd_req_response, sock, fail_auth=False,
149157 traceback .print_exc ()
150158 return
151159 handle_ssh_connection (cmd_req_response , sock , fail_auth = fail_auth ,
152- timeout = timeout )
160+ timeout = timeout , ssh_exception = ssh_exception )
153161
154- def _handle_ssh_connection (cmd_req_response , transport , fail_auth = False ):
162+ def _handle_ssh_connection (cmd_req_response , transport , fail_auth = False ,
163+ ssh_exception = False ):
155164 try :
156165 transport .load_server_moduli ()
157166 except :
158167 return
159168 transport .add_server_key (host_key )
160169 transport .set_subsystem_handler ('sftp' , paramiko .SFTPServer , StubSFTPServer )
161170 server = Server (transport , cmd_req_response = cmd_req_response ,
162- fail_auth = fail_auth )
171+ fail_auth = fail_auth , ssh_exception = ssh_exception )
163172 try :
164173 transport .start_server (server = server )
165174 except paramiko .SSHException , e :
@@ -180,7 +189,7 @@ def _handle_ssh_connection(cmd_req_response, transport, fail_auth=False):
180189 channel .close ()
181190
182191def handle_ssh_connection (cmd_req_response , sock ,
183- fail_auth = False ,
192+ fail_auth = False , ssh_exception = False ,
184193 timeout = None ):
185194 conn , addr = sock .accept ()
186195 logger .info ('Got connection..' )
@@ -190,7 +199,8 @@ def handle_ssh_connection(cmd_req_response, sock,
190199 gevent .Timeout (timeout ).start ()
191200 try :
192201 transport = paramiko .Transport (conn )
193- _handle_ssh_connection (cmd_req_response , transport , fail_auth = fail_auth )
202+ _handle_ssh_connection (cmd_req_response , transport , fail_auth = fail_auth ,
203+ ssh_exception = ssh_exception )
194204 except Exception , e :
195205 logger .error ('*** Caught exception: %s: %s' % (str (e .__class__ ), str (e ),))
196206 traceback .print_exc ()
@@ -200,10 +210,10 @@ def handle_ssh_connection(cmd_req_response, sock,
200210 pass
201211 return
202212
203- def start_server (cmd_req_response , sock , fail_auth = False ,
213+ def start_server (cmd_req_response , sock , fail_auth = False , ssh_exception = False ,
204214 timeout = None ):
205215 return gevent .spawn (listen , cmd_req_response , sock , fail_auth = fail_auth ,
206- timeout = timeout )
216+ timeout = timeout , ssh_exception = ssh_exception )
207217
208218if __name__ == "__main__" :
209219 logging .basicConfig ()
0 commit comments