SSH tunnel from Python is too slow to connect
<p>I'm connecting to a remote SQL database over SSH.  If I set up the SSH connection from the Linux command line (using <code>ssh-add my_private_key.key</code> and then <code>ssh user@mysite.co.uk</code>), it takes less than a second to connect.  But if I do it from Python using <a href="https://github.com/pahaz/sshtunnel" rel="nofollow">sshtunnel</a> (in the following script), it takes around 70 seconds.  I accept that using Python might be a bit of an overhead, but not that much!  And especially since, if I run the Python script <em>after</em> having connected from the command line, it's very fast.  What do I need to add in the script to make it faster?</p>

<p>Python script:</p>

<pre><code>import pymysql, shlex, shutil, subprocess
import logging
import sshtunnel
from sshtunnel import SSHTunnelForwarder
import iot_config as cfg

def OpenRemoteDB():
    global remotecur, remotedb
    sshtunnel.DEFAULT_LOGLEVEL = logging.DEBUG
    with SSHTunnelForwarder(
            (cfg.sshconn['host'], cfg.sshconn['port']),
            ssh_username = cfg.sshconn['user'],
            ssh_private_key = cfg.sshconn['private_key_loc'],
            ssh_private_key_password = cfg.sshconn['private_key_passwd'],
            remote_bind_address = ('127.0.0.1', 3306)) as server:
        print("OK")

# Main program starts here
OpenRemoteDB()
</code></pre>

<p>Python output:</p>

<pre><code>2016-09-20 12:34:15,272 | WARNING | Could not read SSH configuration file: ~/.ssh/config
2016-09-20 12:34:15,305 | INFO    | 0 keys loaded from agent
2016-09-20 12:34:15,332 | DEBUG   | Private key file (/etc/ssh/my_private_key.key, &lt;class 'paramiko.rsakey.RSAKey'&gt;) successfully loaded
2016-09-20 12:34:15,364 | INFO    | Connecting to gateway: mysite.co.uk:22 as user 'user'
2016-09-20 12:34:15,389 | DEBUG   | Concurrent connections allowed: True
2016-09-20 12:34:15,409 | DEBUG   | Trying to log in with key: b'XXX'
2016-09-20 12:35:26,610 | INFO    | Opening tunnel: 0.0.0.0:34504 &lt;&gt; 127.0.0.1:3306
</code></pre>

<p>Doh!  After posting this question I thought it would be a good idea to make sure sshtunnel was up-to-date - and it wasn't.  So I've updated from 0.0.8.1 to the latest version (0.1.0) and my problem is solved!</p>

python linux python-3.x ssh