1
1
# coding: utf-8
2
2
# Copyright (c) Jan Janssen
3
3
4
+ import getpass
4
5
import json
5
6
import os
6
7
import warnings
@@ -25,12 +26,18 @@ def __init__(self, config, directory="~/.queues", execute_command=execute_comman
25
26
)
26
27
if "ssh_key" in config .keys ():
27
28
self ._ssh_key = os .path .abspath (os .path .expanduser (config ["ssh_key" ]))
29
+ self ._ssh_ask_for_password = False
28
30
else :
29
31
self ._ssh_key = None
30
32
if "ssh_password" in config .keys ():
31
33
self ._ssh_password = config ["ssh_password" ]
34
+ self ._ssh_ask_for_password = False
32
35
else :
33
36
self ._ssh_password = None
37
+ if "ssh_ask_for_password" in config .keys ():
38
+ self ._ssh_ask_for_password = config ["ssh_ask_for_password" ]
39
+ else :
40
+ self ._ssh_ask_for_password = False
34
41
if "ssh_key_passphrase" in config .keys ():
35
42
self ._ssh_key_passphrase = config ["ssh_key_passphrase" ]
36
43
else :
@@ -221,15 +228,19 @@ def _transfer_files(self, file_dict, sftp=None, transfer_back=False):
221
228
def _open_ssh_connection (self ):
222
229
ssh = paramiko .SSHClient ()
223
230
ssh .load_host_keys (self ._ssh_known_hosts )
224
- if self ._ssh_key is not None and self ._ssh_key_passphrase is not None :
231
+ if (
232
+ self ._ssh_key is not None
233
+ and self ._ssh_key_passphrase is not None
234
+ and not self ._ssh_ask_for_password
235
+ ):
225
236
ssh .connect (
226
237
hostname = self ._ssh_host ,
227
238
port = self ._ssh_port ,
228
239
username = self ._ssh_username ,
229
240
key_filename = self ._ssh_key ,
230
241
passphrase = self ._ssh_key_passphrase ,
231
242
)
232
- elif self ._ssh_key is not None :
243
+ elif self ._ssh_key is not None and not self . _ssh_ask_for_password :
233
244
ssh .connect (
234
245
hostname = self ._ssh_host ,
235
246
port = self ._ssh_port ,
@@ -240,13 +251,21 @@ def _open_ssh_connection(self):
240
251
self ._ssh_password is not None
241
252
and self ._ssh_authenticator_service is None
242
253
and not self ._ssh_two_factor_authentication
254
+ and not self ._ssh_ask_for_password
243
255
):
244
256
ssh .connect (
245
257
hostname = self ._ssh_host ,
246
258
port = self ._ssh_port ,
247
259
username = self ._ssh_username ,
248
260
password = self ._ssh_password ,
249
261
)
262
+ elif self ._ssh_ask_for_password and not self ._ssh_two_factor_authentication :
263
+ ssh .connect (
264
+ hostname = self ._ssh_host ,
265
+ port = self ._ssh_port ,
266
+ username = self ._ssh_username ,
267
+ password = getpass .getpass (prompt = "SSH Password: " , stream = None ),
268
+ )
250
269
elif (
251
270
self ._ssh_password is not None
252
271
and self ._ssh_authenticator_service is not None
@@ -287,6 +306,16 @@ def authentication(title, instructions, prompt_list):
287
306
ssh ._transport .auth_interactive_dumb (
288
307
username = self ._ssh_username , handler = None , submethods = ""
289
308
)
309
+ elif self ._ssh_ask_for_password and self ._ssh_two_factor_authentication :
310
+ ssh .connect (
311
+ hostname = self ._ssh_host ,
312
+ port = self ._ssh_port ,
313
+ username = self ._ssh_username ,
314
+ password = getpass .getpass (prompt = "SSH Password: " , stream = None ),
315
+ )
316
+ ssh ._transport .auth_interactive_dumb (
317
+ username = self ._ssh_username , handler = None , submethods = ""
318
+ )
290
319
else :
291
320
raise ValueError ("Un-supported authentication method." )
292
321
0 commit comments