SFTP via Paramiko to ipv6 linux machine
<p>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 <strong>Dual Stack Machine (Both IPv4 and IPv6 present)</strong>. Below is the code snippet I am using for Paramiko:</p>

<pre><code>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
</code></pre>

<p>When I use the code with IPv4 it works fine. But when I replace the ip with IPv6 address, following error is thrown:</p>

<pre><code>Traceback (most recent call last):
File "MyFile.py", line 92, in &lt;module&gt;
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 "&lt;string&gt;", line 1, in connect
socket.gaierror: [Errno -2] Name or service not known
</code></pre>

<p>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:</p>

<pre><code>Traceback (most recent call last):
File "MyFile.py", line 92, in &lt;module&gt;
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 "&lt;string&gt;", line 1, in connect
socket.gaierror: [Errno -9] Address family for hostname not supported
</code></pre>

<p>My original server will not be a dual stack machine and hence I need the file transfer via IPv6 only. </p>

<p><strong>NOTE</strong>:When I use sftp command in linux, it works for both ipv4 and ipv6</p>

<p>Any possible solution or additional suggestions would be really appreciated</p>

<p>Paramiko's <code>Transport</code> class supports passing in a socket object as well as a tuple. So maybe try specifically passing in an ipv6 socket?</p>

<pre><code>import socket

sock = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
sock.connect((hostname, port))
transport = paramiko.Transport(sock)
</code></pre>

python linux python-2.7 paramiko