How to set up connection to AWS rds via ssh to server from python? - ssh

I have to access an aws rds from a remote server. So far I have had no problems accessing the mysql databases from the command line.
I ssh to the server like: ssh username#XX.XXX.XXXX.XX
Once I'm in the server I use mysql -hrds_host.amazonaws.com -uuser -ppassword
And then I am able to call sql queries for the different databases from my terminal.
I have been trying to set up this connection through python using an ssh tunnel but fail to do so successfully. I expect the code to look something like:
with SSHTunnelForwarder(
(server_host, 22),
ssh_username=username,
ssh_private_key=private_key,
remote_bind_address=(rds_host, 3306)
) as server:
conn = sql.connect(host=rds_host,
port=server.local_bind_port,
user=user,
passwd=password,
db=database)
I continuously get errors like:
Could not establish session to SSH gateway
or An error occurred while opening tunnels.
The logging level of my SSHTunnelForwardes always shows an error.
I have also been successful setting up a connection with a paramiko ssh.client, just not the tunnel to call queries.

conn = sql.connect(host=rds_host,
port=server.local_bind_port,
user=user,
Change this connection to localhost, it should work

Related

how to check ssh tunnel opened by a Dbeaver client for a pg_dump task?

i actually use a dbeaver client to connect to a remote Postgres database.
dbeaver client use a bastion, by creating a ssh tunnel between localhost and bastion
(and then, bastion is connected to the database).
We have something like that
localhost (dBeaver) --> bastion --> postgres database
to make a backup of my database, dbeaver client use a command like :
pg_dump --verbose --host=127.0.0.1 --port=59914 --username=my-user --format=c -t public.dashboard my-database > /dump.sql
so, i deduce that dbeaver created a tunnel between my localhost 55914 to the remote port of the postgres database (5432)
All is well and works perfectly, but i'd like to understand how dbeaver created the ssh tunnel.
How can we check in Linux that it exist an opened tunnel from my 59914 local port to the remote postgres database, using interdemediary bastion machine ?
What is the command line to check that ?

Double tunnel hop ssh

I'm using WinSSHTerm to connect to a proxy, from which I then connect to a server hosting a data warehouse. I just can't figure out how to reproduce my Putty connection using a shell command.
Short recap:
I first connect to the proxy server which maps the port 5432 to my local port 10001. After that, i connect to the database server and map its 5432 port to my proxy's 5432 port, which I previously mapped to my 10001 port locally. I am then able to connect to the databse via a database manager locally.
To do so:
I created the following connection to my proxy server first.
I then added a tunnel from there to my localhost port 10001.
Once I'm logged in to the proxy server, I use the following command to connect to the database server and map its 5432 port to the proxy's 5432 port.
ssh username#databaseServer -L 127.0.0.1:5432:databaseServer:5432
I'd like to leave putty and move to WinSSHterm, predefine some login commands for a specific server.
How may I reproduce the behavior above using a shell command?
Here's my initial try, which is unfortunately not working:
ssh username#databaseServer -L 127.0.0.1:5432:databaseServer:5432
Thank you
I was finally able to find the correct way to write it.
Loginc Cmds
ssh username#databaseServer -L 127.0.0.1:5432:databaseServer:5432
Cmd-line Args
-L 10001:localhost:5432

SCP times out, but ssh connection works fine. Am I doing something wrong?

I'm trying to copy task1.zip from my desktop /Users/myname/desktop if I pwd, to a remote server. I'm connected to the remote server via ssh. I would like to copy the file to /its/home/jt463/task1(pwd path from the directory) on the remote server.
I have used the command below in the terminal when I'm connected to the server via ssh and tried it on the terminal on my machine:
scp Users/myname/desktop/task1.zip username#inf900179.inf.susx.ac.uk:its/home/username/task1
Error that I get when I try to use the terminal that's connected to the remote server:
Users/jonatantibarovsky/desktop/task1.zip: No such file or directory
Error that I get when I try to use my local terminal:
ssh: connect to host inf900179.inf.susx.ac.uk port 22: Operation timed out lost connection
First scp to the intermediate server, using your credentials. Then, you should be able to scp from that server to the target.

Access the database from outside using a SSH tunnel Fortrabbit

I copy command from form Fortrabbit to access my database using a SSH tunnel:
#Access the database from outside using a SSH tunnel
ssh -N -L 13306:myapp.mysql.eu1.frbit.com:3306 u-my-app#ssh2.eu1.frbit.com
I entered my passphrase. But when I hit enter nothing happens.
Could anyone help?
You have set up a port forwarding from your localhost:3306 to remoteport:3306.
What this means is the mysql database runinng on remote machine is now accessible at your localhost port 3306.
You can connect it using
Shell
mysql -u <username> -p<password> --host=127.0.0.1 --port=3306
JDBC
String url = "jdbc:mysql://localhost/<db_name>";
Class.forName ("com.mysql.jdbc.Driver").newInstance ();
Connection conn = DriverManager.getConnection (url, "username", "password");
This will only work as long as the SSh session opened previosly is alive.
That nothing seems to happen is a good sign:
The connection seems established, and you can, in a separate terminal window, use that existing connection for your mysql work.
See also the fortrabbit tutorial on mysql connection:
https://help.fortrabbit.com/mysql

How can I use SSH tunneling to connect to a remote MySQL server?

I'm using SSH tunneling for the first time, so I'm trying to understand how to configure it.
I've got a remote Linux server that hosts a MySQL database that I'm trying to connect to. In order to access the MySQL database directly through a software that only recognizes local databases, I suppose SSH tunneling would be the right way to set up the access, correct?
Now, I'm trying to set up the tunneling on my 'home' computer which is running the software that's trying to access the MySQL database. My first question is whether this is reverse tunneling or normal tunneling? Secondly, is it local tunneling or remote tunneling?
Finally, from what I understand, my code is supposed to look something like
ssh -L 8080:mylinuxserver.mycompany.com:22 myuser#mylinuxserver.mycompany.com
Is this correct? Is my source port '22' since I'm using SSH and is my destination port 8080 (or is there something more appropriate)?
When I try to use the above code, I am able to login using my passphrase (since my key is already in the MyLinuxServer) but when I ping localhost:8080, it cannot find the host.
What am I doing wrong?
I've got a remote Linux server that hosts a MySQL database that I'm trying to connect to
The command should be:
ssh -L 8080:localhost:3306 myuser#mylinuxserver.mycompany.com
Where:
8080: is hte local port on your workstation
localhost: is corresponding to mylinuxserver.mycompany.com
3306: the MySQL port on above localhost.
then connect (from your workstation) to MySQL as:
mysql -h 127.0.0.1 --port=8080
Besides, ping localhost:8080 is wrong. Ping cannot work that way.
Try this:
ssh -f ssh_user#mylinuxserver.mycompany.com -L 3307:mysql1.example.com:3306 -N
Next, to access the mysql your trying to connect to:
mysql -h 127.0.0.1 -P 3307