0

I connect to my db manually using these steps:

1>load a putty session with ip 1.1.1.1 and port 1111  
2>login as: login1  
3>[email protected]'s password: pwd1  
4>[login1@G ~]$ ssh [email protected]  
5>[login1@l ~]$ MySQL -h 3.3.3.3 -u login2 -p'pwd2' -D mydb 

Now I can can query anything successfully like select * from my_table.

I have a python selenium webdriver code from where I want to read my db. I am not able to achieve it because of ssh tunnelling and 3 IP's involved.

from sshtunnel import SSHTunnelForwarder
import MySQLdb

with SSHTunnelForwarder(
         ('host', 1111),
         ssh_password="pwd1
         ssh_username="login1",
         remote_bind_address=('2.2.2.2', 1111)) as server:

    con = None
    con = MySQLdb.connect(user='login2',passwd='pwd2',db='mydb',host=3.3.3.3,port=3306)
    cur = con.cursor()

I am getting this error:

BaseSSHTunnelForwarderError: Could not resolve IP address for %s, aborting!

1 Answer 1

1

When instantiating SSHTunnelForwarder:

The param host is your remote ssh 2.2.2.2.
Argument for remote_bind_address will be for the tunnel, the port is the one you want to tunnel, so your mysql port. ('3.3.3.3', 3306)

Then when connecting to mysql, you want it to pass though the tunnel to access the mysql instance. The tunnel port is server.local_bind_port.

from sshtunnel import SSHTunnelForwarder
import MySQLdb

with SSHTunnelForwarder(
    ('2.2.2.2', 1111),
    ssh_password="pwd1"
    ssh_username="login1",
    remote_bind_address=('3.3.3.3', 3306)) as server:

    con = MySQLdb.connect(
        user='login2',passwd='pwd2',
        db='mydb',host=1.1.1.1,
        port=server.local_bind_port)
Sign up to request clarification or add additional context in comments.

10 Comments

2015-10-08 19:59:26,322 | WARNING | Could not read SSH configuration file: ~/.ssh/config 2015-10-08 19:59:26,375 | INFO | Connecting to gateway: 2.2.2.2:1111 as user "login1". 2015-10-08 19:59:47,375 | ERROR | Could not connect to gateway: 2.2.2.2 Traceback (most recent call last): File "D:\API.py", line 23, in <module> remote_bind_address=('3.3.3.3', 3306)) as server: File "C:\Python27\lib\site-packages\sshtunnel.py", line 626, in init raise BaseSSHTunnelForwarderError(msg) BaseSSHTunnelForwarderError: Could not connect to gateway: 2.2.2.2
Could not read SSH configuration file: ~/.ssh/config. probably a permission issue.
Does it means I should have one of the ip in my host file?
Sadly sshtunel mute the original error. Could you transform this line in C:\Python27\lib\site-packages\sshtunnel.py into except paramiko.SSHException as err: msg = 'Could not connect to gateway: {0}\n{1}'.format(ssh_host, err)
Unable to connect to 2.2.2.2: [Errno 10060] A connection attempt failed because the connected party did not properly respond after a period of time or established connection failed because connected host has failed to respond
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.