'Unable to Connect to SFTP through paramiko

I am trying to establish a connection with an SFTP with paramiko. I was able to generate the known_hosts file in my local system by using

ssh my.domain.com

The resultant file has both the host and its IP in the first line of known_hosts, like

my.domain.com,xx.xx.xxx.xx ...

When I try to connect through paramiko,

host, port = 'my.domain.com,xx.xx.xxx.xx', 22
user, pwd = "xyz", "abc"

ssh = paramiko.SSHClient()
ssh.connect(host, port, username=user, password=pwd)

I get the error

socket.gaierror: [Errno 11001] getaddrinfo failed

After looking this up, the solutions were to not mention user in host or add port, etc. But I'm still not able to connect. I tried removing my.domain.com from both the Python code and known_hosts file,

host, port = 'xx.xx.xxx.xx', 22  
user, pwd = "xyz", "abc"

ssh = paramiko.SSHClient()
ssh.connect(host, port, username=user, password=pwd)

but that didn't work. I tried removing xx.xx.xxx.xx from both the Python code and known_hosts file,

host, port = 'xx.xx.xxx.xx', 22
user, pwd = "xyz", "abc"

ssh = paramiko.SSHClient()
ssh.connect(host, port, username=user, password=pwd)

but that didn't work either.

How do I connect to my SFTP?



Solution 1:[1]

As resolved by @tdalaney in the comments. I edited the known_hosts file to only specify the domain name and added ssh.load_system_host_keys() before the connect method.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mostafa Wael