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?

1 answer

  • answered 2022-05-07 08:59 python_amateur

    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.

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum