Skip to content

Commit

Permalink
Merge pull request #1905 from KenyC/main
Browse files Browse the repository at this point in the history
Changed openssh password prompt regex + Added max number of retries ; fix #1904
  • Loading branch information
minrk authored Oct 13, 2023
2 parents 96ad4c5 + 9e6d1a7 commit 72ba0d5
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions zmq/ssh/tunnel.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ class SSHException(Exception): # type: ignore
pexpect = None


class MaxRetryExceeded(Exception):
pass


def select_random_ports(n):
"""Select and return n random ports that are available."""
ports = []
Expand All @@ -56,7 +60,7 @@ def select_random_ports(n):
# -----------------------------------------------------------------------------
# Check for passwordless login
# -----------------------------------------------------------------------------
_password_pat = re.compile(rb'pass(word|phrase):', re.IGNORECASE)
_password_pat = re.compile(rb'pass(word|phrase)', re.IGNORECASE)


def try_passwordless_ssh(server, keyfile, paramiko=None):
Expand Down Expand Up @@ -90,7 +94,10 @@ def _try_passwordless_openssh(server, keyfile):

ssh_newkey = 'Are you sure you want to continue connecting'
p = pexpect.spawn(cmd, env=env)
while True:

MAX_RETRY = 10

for _ in range(MAX_RETRY):
try:
i = p.expect([ssh_newkey, _password_pat], timeout=0.1)
if i == 0:
Expand All @@ -104,6 +111,8 @@ def _try_passwordless_openssh(server, keyfile):
else:
return False

raise MaxRetryExceeded(f"Failed after {MAX_RETRY} attempts")


def _try_passwordless_paramiko(server, keyfile):
"""Try passwordless login with paramiko."""
Expand Down Expand Up @@ -274,7 +283,8 @@ def openssh_tunnel(
ssh_newkey = 'Are you sure you want to continue connecting'
tunnel = pexpect.spawn(cmd, env=env)
failed = False
while True:
MAX_RETRY = 10
for _ in range(MAX_RETRY):
try:
i = tunnel.expect([ssh_newkey, _password_pat], timeout=0.1)
if i == 0:
Expand All @@ -299,6 +309,7 @@ def openssh_tunnel(
password = getpass("%s's password: " % (server))
tunnel.sendline(password)
failed = True
raise MaxRetryExceeded(f"Failed after {MAX_RETRY} attempts")


def _stop_tunnel(cmd):
Expand Down

0 comments on commit 72ba0d5

Please sign in to comment.