Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changed openssh password prompt regex + Added max number of retries ; fix #1904 #1905

Merged
merged 1 commit into from
Oct 13, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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