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>login1@1.1.1.1's password: pwd1  
4>[login1@G ~]$ ssh login1@2.2.2.2  
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!

Saurabh Shrivastava
  • 1,394
  • 4
  • 21
  • 50

1 Answers1

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)
Cyrbil
  • 6,341
  • 1
  • 24
  • 40
  • 1
    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 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 – Saurabh Shrivastava Oct 08 '15 at 14:31
  • `Could not read SSH configuration file: ~/.ssh/config`. probably a permission issue. – Cyrbil Oct 08 '15 at 14:43
  • Does it means I should have one of the ip in my host file? – Saurabh Shrivastava Oct 08 '15 at 14:53
  • 1
    Sadly `sshtunel` mute the original error. Could you transform [this line](https://github.com/pahaz/sshtunnel/blob/master/sshtunnel.py#L634) 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)` – Cyrbil Oct 08 '15 at 15:00
  • 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 – Saurabh Shrivastava Oct 08 '15 at 15:09
  • Any idea about this error. I read somewhere to add ip in host file, but which ip from 3 ip listed. – Saurabh Shrivastava Oct 08 '15 at 15:17
  • If you can connect manually, I doubt messing with host file will do something. – Cyrbil Oct 08 '15 at 15:19
  • any other suggestion which I should try. I can connect very smoothly manually. – Saurabh Shrivastava Oct 08 '15 at 15:21
  • Put `logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)` at the beginning of your code to output debug information including sshtunel. – Cyrbil Oct 08 '15 at 15:25
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/91766/discussion-between-cyrbil-and-user3388005). – Cyrbil Oct 08 '15 at 15:25