paramiko / paramiko/paramiko

Issue connecting by DNS name with ".connect"

Open
#1,712 10 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
9.9k
Forks
2.1k
PR merge metrics
No merged PRs in 30d

Description

I have a small program I'm trying to use and connect to a linux/unix box with SSHClient. In this specific case, the remote system is a Raspberry Pi running raspbian (which I'm pretty sure has NO bearing on the issue).

My local network has a caching DNS that I use for internal addresses - I do not modify /etc/hosts, etc. In this case, ARP is used for the system to announce itself, and the caching DNS on my router will cache the name and IP as .local

I can always drop to a shell, and use the installed ssh client to connect to this system using ssh pi@.local without fail.

However, if I try to connect using:
client.connect('.local', username=user, password=pass, port=22)
will ALWAYS timeout.

If I use the IP address:
client.connect('10.0.0.50', username=user, password=pass, port=22)
it will IMMEDIATELY connect without issue.

If I modify this slightly like:
dns = socket.gethostbyname(".local")
client.connect(dns.strip', username=user, password=pass, port=22)
It will again immediately connect without issue

So this isn't a case of python not being able to resolve system names within it's libraries - this appears to be something within paramiko itself.

Here's the full sample code:

import paramiko
import socket
import getpass
import sys

dns = socket.gethostbyname("somesystem.local")

user="<username>"
pass="<password>"

client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())

try:
#    client.connect('10.0.0.53', username=user, password=pass, port=22)
#    client.connect(socket.gethostbyname("somesystem.local"), username=user, password=pass, port=22)
    client.connect(dns.strip(), username=user, password=pass, port=22)
    print("Success on connection")
except paramiko.SSHException:
    print("Connection Failed")
    quit()

stdin, stdout, stderr = client.exec_command("ls -la; df -h")
for line in stdout.readlines():
    print (line.strip())

client.close()

del client, stdout, stderr
sys.exit()

sys.exit()

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at Paramiko's SSHClient.connect entry point and compare the reported behavior for the .local hostname with the socket.gethostbyname result shown in the sample. Reproduce the timeout using the provided code and verify that connecting by hostname succeeds consistently without manually resolving it first.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
networking
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.