I am relatively new to python and am trying sftp for the first time via python script. I want my python script to get a file from a Dual Stack Machine (Both IPv4 and IPv6 present). Below is the code snippet I am using for Paramiko:
host = ip #ip is a string that has the value of IP port = 22 transport = paramiko.Transport((host, port)) transport.connect(username = username, password = password) sftp = paramiko.SFTPClient.from_transport(transport
When I use the code with IPv4 it works fine. But when I replace the ip with IPv6 address, following error is thrown:
Traceback (most recent call last): File "MyFile.py", line 92, in <module> putFile() File "MyFile.py", line 29, in analyzeLogs transport = paramiko.Transport((host, port)) File "/usr/lib/python2.6/site-packages/paramiko/transport.py", line 289, in __init__ sock.connect((hostname, port)) File "<string>", line 1, in connect socket.gaierror: [Errno -2] Name or service not known
I checked for a solution and found someone suggesting to add the interface along with the IP but while trying the same I got the following error:
Traceback (most recent call last): File "MyFile.py", line 92, in <module> putFile() File "MyFile.py", line 29, in analyzeLogs transport = paramiko.Transport((host, port)) File "/usr/lib/python2.6/site-packages/paramiko/transport.py", line 289, in __init__ sock.connect((hostname, port)) File "<string>", line 1, in connect socket.gaierror: [Errno -9] Address family for hostname not supported
My original server will not be a dual stack machine and hence I need the file transfer via IPv6 only.
NOTE:When I use sftp command in linux, it works for both ipv4 and ipv6
Any possible solution or additional suggestions would be really appreciated
Advertisement
Answer
Paramiko’s Transport
class supports passing in a socket object as well as a tuple. So maybe try specifically passing in an ipv6 socket?
import socket sock = socket.socket(socket.AF_INET6, socket.SOCK_STREAM) sock.connect((hostname, port)) transport = paramiko.Transport(sock)