Skip to content

SshClient.connect() - not failing over IPv6 to IPv4 when IPv6 address is unreachable. #686

Open

Description

@gwtp

Example Scenario:

  • Hostname has both IPv4 and IPv6 address.
  • IPv6 address is unreachable,
  • IPv4 address is reachable.
  • Paramiko tries the IPv6 address first (hangs for couple of minutes) and then times out. No fail over to IPv4 occurs. Output below.

Additionally when specifying hostname as the IPv4 address, the connection is instantly successful.

Exception raised:

client.connect(hostname=hostname, username=username, password=password, allow_agent=False)
Traceback (most recent call last):
File "", line 1, in
File "/usr/local/lib/python2.7/dist-packages/paramiko/client.py", line 292, in connect
retry_on_signal(lambda: sock.connect(addr))
File "/usr/local/lib/python2.7/dist-packages/paramiko/util.py", line 270, in retry_on_signal
return function()
File "/usr/local/lib/python2.7/dist-packages/paramiko/client.py", line 292, in
retry_on_signal(lambda: sock.connect(addr))
File "/usr/lib/python2.7/socket.py", line 224, in meth
return getattr(self._sock,name)(*args)
socket.error: [Errno 110] Connection timed out

Activity

  1. bitprophet commented on Jul 2, 2016

    @bitprophet
    Member

    Thanks for the report!

    This may be a bug in the connection code, which is supposed to try all possible versions of the requested address in order, optionally with an explicit timeout (which you can pass to .connect()). See

    paramiko/paramiko/client.py

    Lines 296 to 316 in 0ae019c

    to_try = list(self._families_and_addresses(hostname, port))
    for af, addr in to_try:
    try:
    sock = socket.socket(af, socket.SOCK_STREAM)
    if timeout is not None:
    try:
    sock.settimeout(timeout)
    except:
    pass
    retry_on_signal(lambda: sock.connect(addr))
    # Break out of the loop on success
    break
    except socket.error as e:
    # Raise anything that isn't a straight up connection error
    # (such as a resolution error)
    if e.errno not in (ECONNREFUSED, EHOSTUNREACH):
    raise
    # Capture anything else so we know how the run looks once
    # iteration is complete. Retain info about which attempt
    # this was.
    errors[addr] = e
    which is the relevant code snippet.

    Going by the Python stdlib docs for getaddrinfo, IPv4 probably pops up before IPv6 most of the time, which would be why it's tried first and is then gumming up the works.

    At a glance, I'm thinking that this code was never actually tried in the "IPv4 times out, instead of being outright unreachable/unroutable" scenario, since the exception handling only skips unreachable/unconnectable errors - not timeouts. The fix may thus be as simple as updating that except line.

    So either @gwtp needs to be comfortable testing a PR, or I (or another contributor) need to find/create a reproducible network case triggering this.

  2. added this to the milestone on Jul 2, 2016
  3. modified the milestones: 1.17.3 et al, on Dec 9, 2016
  4. Poply commented on Nov 15, 2017

    @Poply

    Had this same issue when using paramiko 2.4. I could .connect() fine when the host resolved to a single IPv4 address, but if it also had an IPv6 address associated I got a timeout error.

    Had to use socket.gethostbyname(myhostname) as a workaround when connecting.

  5. wsw70 commented on Jun 28, 2024

    @wsw70

    The problem is still present in 3.4.0.
    @Poply workaround to use hostname=socket.gethostbyname(myhostname) works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions