"getaddrinfo failed" when connecting to SFTP server using Paramiko - python

I am trying to connect to SFTP server but it returns error:
[Errno 11001] getaddrinfo failed
I am using Python 3.7.3 and Paramiko version is 2.6.0
import paramiko
host_name = "sftp://81.149.151.143"
user_name = "******"
password = "******"
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(hostname=host_name, port=220, username=user_name, password=password)
ftp_client=ssh_client.open_sftp()
ftp_client.put('***/issue_1.docx', '/issue_1.docx')
ftp_client.close()
This is the full error:
Traceback (most recent call last):
File "/sftp/paramiko_bot.py", line 10, in <module>
ssh_client.connect(hostname=host_name, port=22, username=user_name, password=password)
File "\Local\Programs\Python\Python37\lib\site-packages\paramiko\client.py", line 340, in connect
to_try = list(self._families_and_addresses(hostname, port))
File "\Local\Programs\Python\Python37\lib\site-packages\paramiko\client.py", line 204, in _families_and_addresses
hostname, port, socket.AF_UNSPEC, socket.SOCK_STREAM
File "\AppData\Local\Programs\Python\Python37\lib\socket.py", line 748, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 11001] getaddrinfo failed

The hostname parameter of SSHClient.connect should contain a hostname only (or in your case an IP address) – not any kind of URL.
ssh_client.connect(hostname="81.149.151.143", port=220, username=..., password=...)
Obligatory warning: Do not use AutoAddPolicy this way – You are losing a protection against MITM attacks by doing so. For a correct solution, see Paramiko "Unknown Server".

Related

How can I connect to a FTP server in Python? [duplicate]

This question already has an answer here:
FTPLIB error socket.gaierror: [Errno 8] nodename nor servname provided, or not known [duplicate]
(1 answer)
Closed 1 year ago.
I am having problems when I try to connect to a ftp server, I am using a Mac with BigSur and Python3.
Here is my code:
import ftplib
FTP_HOST = "ftp://myserver"
FTP_USER = "user"
FTP_PASS = "pass"
# connect to the FTP server
ftp = ftplib.FTP(FTP_HOST, FTP_USER, FTP_PASS)
Here what it returns:
Traceback (most recent call last):
File "/Users/adrianrodriguezgalisteo/jonsuGo/test_upload.py", line 8, in <module>
ftp = ftplib.FTP(FTP_HOST, FTP_USER, FTP_PASS)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/ftplib.py", line 119, in __init__
self.connect(host)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/ftplib.py", line 156, in connect
self.sock = socket.create_connection((self.host, self.port), self.timeout,
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/socket.py", line 822, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/socket.py", line 953, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 8] nodename nor servname provided, or not known
I just want to connect to the server and upload a file.
Thank you.
https://docs.python.org/3/library/ftplib.html
The host should only be myserver
FTP_HOST = "myserver"
You may need the FQDN of the servername

How to use Boto3 S3 connection with proxy?

I want to write the Python script to download from S3 bucket using Boto3. My code is working fine when not using proxy, but I have to run this under proxy. I have set proxy in the Boto3.client(config=) and I get the following error.
Proxy set as:
s3 = boto3.client('s3',
aws_access_key_id = usrkey,
aws_secret_access_key = sctkey,
region_name = 'eu-west-2',
config=Config(proxies={'http': '<proxy-server>:<port>'})
)
still end up with the error
Traceback (most recent call last):
File "C:\Users\Hello\AppData\Local\Programs\Python\Python38-32\lib\site-packages\urllib3\connection.py", line 159, in _new_conn
conn = connection.create_connection(
File "C:\Users\Hello\AppData\Local\Programs\Python\Python38-32\lib\site-packages\urllib3\util\connection.py", line 61, in create_connection
for res in socket.getaddrinfo(host, port, family, socket.SOCK_STREAM):
File "C:\Users\Hello\AppData\Local\Programs\Python\Python38-32\lib\socket.py", line 918, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 11001] getaddrinfo failed
During handling of the above exception, another exception occurred:
and at the end:
botocore.exceptions.EndpointConnectionError: Could not connect to the endpoint URL:"<URL>".
I can connect using WinSCP to AWS-S3, with proxy, that is working fine. But it pops up a warning message that I have to accept every time, by clicking ok. What is the problem here, I don't understand with Python.
Make sure your <proxy-server> is a host name:
config=Config(proxies={'http': 'example.com:port'}
Not a URL:
config=Config(proxies={'http': 'http://example.com:port'}

Unable to connect to server using Paramiko package

I'm trying to connect to my server using SSH with port 2022 (not 22) in Python. So I wrote the following code that uses Paramiko package:
import sys
import paramiko
try:
client = paramiko.SSHClient()
client.load_system_host_keys()
client.set_missing_host_key_policy(paramiko.WarningPolicy)
client.connect('ccap#10.40.2.222', '2022', '', 'ccap')
finally:
client.close()
But when I'm running it in my IDE (PyCharm) I get the following error:
/usr/local/lib/python3.5/dist-packages/paramiko/ecdsakey.py:164: CryptographyDeprecationWarning: Support for unsafe construction of public numbers from encoded data will be removed in a future version. Please use EllipticCurvePublicKey.from_encoded_point
self.ecdsa_curve.curve_class(), pointinfo
Traceback (most recent call last):
File "/home/mshapirs/PycharmProjects/OnlineTest.py/OnlineTest.py", line 9, in
client.connect('ccap#10.40.2.222', '2022', '', 'ccap')
File "/usr/local/lib/python3.5/dist-packages/paramiko/client.py", line 334, in connect
to_try = list(self._families_and_addresses(hostname, port))
File "/usr/local/lib/python3.5/dist-packages/paramiko/client.py", line 204, in _families_and_addresses
hostname, port, socket.AF_UNSPEC, socket.SOCK_STREAM
File "/usr/lib/python3.5/socket.py", line 733, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno -2] Name or service not known
You should provide username as a separate parameter, not prepended to the host address.
Look at the docs for .connect. It has username and hostnamelisted separately.

Receiving getaddrinfo [Errno -2] when trying to use Beaglebone Black to send email

I'm trying to use a Beaglebone Black (BBB) to send email notifications, but I'm getting caught up on this getaddrinfo error that reads as follows;
socket.gaierror: [Errno -2] Name or service not known
I've been working on this for a while and can't find why this isn't working.
The nano file I"m trying to run:
import smtplib
#import time
smtpObj = smtplib.SMTP('smtp.gmail.com', 587)
#time.sleep(1000)
print("SMTP object created...")
smtpObj.ehlo()
#time.sleep(1000)
print("EHLO...")
smtpObj.starttls()
#time.sleep(1000)
print("Starting TLS...")
smtpObj.login('EXAMPLEACCOUNT#gmail.com', 'EXAMPLEPASSWORD')
#time.sleep(1000)
print("Logged into EXAMPLEACCOUNT#gmail.com...")
smtpObj.sendmail('EXAMPLEACCOUNT#gmail.com', 'EXAMPLERECIPIENT', '''Subject:test subject \ntest body
Auto Alert System.''')
{}
#time.sleep(1000)
print("Sending email...")
smtpObj.quit()
#time.sleep(1000)
print("Destorying object.")
The output of invoking the test_email2.py function is as follows:
root#beaglebone:~/Desktop/email_project# python test_email2.py
Traceback (most recent call last):
File "test_email2.py", line 4, in <module>
smtpObj = smtplib.SMTP('smtp.gmail.com', 587)
File "/usr/lib/python2.7/smtplib.py", line 249, in __init__
(code, msg) = self.connect(host, port)
File "/usr/lib/python2.7/smtplib.py", line 309, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/usr/lib/python2.7/smtplib.py", line 284, in _get_socket
return socket.create_connection((port, host), timeout)
File "/usr/lib/python2.7/socket.py", line 553, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
socket.gaierror: [Errno -2] Name or service not known
The format I've been following is based on that provided by https://automatetheboringstuff.com/chapter16/
socket.gaierror means that (underlying in libc) getaddrinfo function failed to get IP addresses for domain names you provided. It explains why it failed: [Errno -2] Name or service not known, so it doesn't know about a domain with such a name, smtp.gmail.com. This domain name obviously exists, so you should look into DNS system settings in your BBB system (and it's actually more of a SuperUser community question).
What DNS servers are used in configuration? If you're using a local caching DNS server at loopback, is it up and running? Is it configured properly to allow recursive requests? This particular problem most likely has nothing to do with Python or your code; it's your BBB system cannot resolve at least some, if not all, domain names.

Socket error when connecting to remote machine with Python

I am using Python's paramiko package to connect to a remote Unix machine. I have written this code:
import paramiko
import sys
import os
import os.path
passwd = "XXX"
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect("173.15.13.28", "root", password=passwd)
stdin, stdout, stderr = ssh.exec_command('ls')
x = stdout.readlines()
print x
for line in x:
print line
ssh.close()
after executing I am getting this error:
Traceback (most recent call last):
File "<pyshell#9>", line 1, in <module>
ssh.connect("173.15.13.28", "root", password="--------")
File "C:\Python27\lib\site-packages\paramiko\client.py", line 282, in connect
for (family, socktype, proto, canonname, sockaddr) in socket.getaddrinfo(hostname, port,socket.AF_UNSPEC, socket.SOCK_STREAM):
gaierror: [Errno 10109] getaddrinfo failed
I don't know what the problem is.
The second argument of the connect() method is the port number, which defaults to 22. You are putting "root" there, which won't work. Use the keyword argument username="root". That is, try this:
ssh.connect("173.15.13.28", username="root", password=passwd)
See also: python paramiko, getaddrinfo error when trying to establish an SSH connection

Categories

Resources