Skip to content

Commit fe15ed8

Browse files
authored
Merge pull request #263 from pyiron/getpass
Ask for SSH password using getpass
2 parents ef4b08a + 31be443 commit fe15ed8

File tree

1 file changed

+31
-2
lines changed

1 file changed

+31
-2
lines changed

pysqa/ext/remote.py

+31-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# coding: utf-8
22
# Copyright (c) Jan Janssen
33

4+
import getpass
45
import json
56
import os
67
import warnings
@@ -25,12 +26,18 @@ def __init__(self, config, directory="~/.queues", execute_command=execute_comman
2526
)
2627
if "ssh_key" in config.keys():
2728
self._ssh_key = os.path.abspath(os.path.expanduser(config["ssh_key"]))
29+
self._ssh_ask_for_password = False
2830
else:
2931
self._ssh_key = None
3032
if "ssh_password" in config.keys():
3133
self._ssh_password = config["ssh_password"]
34+
self._ssh_ask_for_password = False
3235
else:
3336
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
3441
if "ssh_key_passphrase" in config.keys():
3542
self._ssh_key_passphrase = config["ssh_key_passphrase"]
3643
else:
@@ -221,15 +228,19 @@ def _transfer_files(self, file_dict, sftp=None, transfer_back=False):
221228
def _open_ssh_connection(self):
222229
ssh = paramiko.SSHClient()
223230
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+
):
225236
ssh.connect(
226237
hostname=self._ssh_host,
227238
port=self._ssh_port,
228239
username=self._ssh_username,
229240
key_filename=self._ssh_key,
230241
passphrase=self._ssh_key_passphrase,
231242
)
232-
elif self._ssh_key is not None:
243+
elif self._ssh_key is not None and not self._ssh_ask_for_password:
233244
ssh.connect(
234245
hostname=self._ssh_host,
235246
port=self._ssh_port,
@@ -240,13 +251,21 @@ def _open_ssh_connection(self):
240251
self._ssh_password is not None
241252
and self._ssh_authenticator_service is None
242253
and not self._ssh_two_factor_authentication
254+
and not self._ssh_ask_for_password
243255
):
244256
ssh.connect(
245257
hostname=self._ssh_host,
246258
port=self._ssh_port,
247259
username=self._ssh_username,
248260
password=self._ssh_password,
249261
)
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+
)
250269
elif (
251270
self._ssh_password is not None
252271
and self._ssh_authenticator_service is not None
@@ -287,6 +306,16 @@ def authentication(title, instructions, prompt_list):
287306
ssh._transport.auth_interactive_dumb(
288307
username=self._ssh_username, handler=None, submethods=""
289308
)
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+
)
290319
else:
291320
raise ValueError("Un-supported authentication method.")
292321

0 commit comments

Comments
 (0)