Ping target through ssh tunnel - ping

I want to connect from source server S to my target server T. However connection to T is restricted to only from intermediate server I. Since S can't connect to T, I have created a ssh tunnel from S to T via I using:
ssh -N -f -L port:T:22 I
Now I can connect to T using:
ssh -p port user#localhost
But my problem is that I have to ping to the target T. How can I ping using tunneling.

You can't ping through a tunnel. SSH tunnels can only pass TCP connections, ping uses ICMP.
You could run ping on the intermediate server via SSH:
ssh I ping T

Related

ssh port forward to mysql from R

I'm able to connect to my company's MySQL server via SSH in Terminal
ssh -L 3306:localhost:3306 me#jumphost.mycompany.com
mysql -h mysql.mycompany.com -ume -pmy_password
I'm struggling to find a way to do this in an R Script. Any suggestions appreciated.
If I try to connect using DBI (after connecting to ssh in Terminal):
con <- DBI::dbConnect(RMariaDB::MariaDB(),
host = "localhost",
user = "me",
password = "my_password")
I get this error: Error: Failed to connect: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
Use 127.0.0.1 instead of localhost.
If you use localhost, the client does not connect to port 3306 using TCP. It tries to connect to a UNIX socket, which only reaches an instance running on your client host.
I answered the same question about MySQL in the past (Accessing SQL through SSH Tunnel). MariaDB is not MySQL, but in this case they should work the same.
Since you can successfully run the mysql CLI in same session as ssh port forwarding, consider running R at command line as well using its CLI, Rscript, or the shell, R. Alternatively, try running the SSH port forwarding directly inside R code using the command line caller, system.
The challenge you are encountering is the SSH tunneling occurs in a different session to your R session. Your R environment must run in the same session as SSH port forwarding.
Bash Command Line (using Rscript)
ssh -L 3306:localhost:3306 me#jumphost.mycompany.com
Rscript my_database_connect_script.R
# OR /path/to/R/installation/bin/Rscript my_database_connect_script.R
ssh -O cancel -L 3306:localhost:3306 me#jumphost.mycompany.com
R script (using system)
library(DBI)
library(RMariaDB)
# COMMAND LINE INTERFACE CALL
system("ssh -L 3306:localhost:3306 me#jumphost.mycompany.com")
# OPEN DB CONNECTION
con <- DBI::dbConnect(
RMariaDB::MariaDB(),
host = "mysql.mycompany.com", # SAME HOST AS SUCCESSFUL mysql CLI
user = "me",
password = "my_password"
)
dbGetQuery(con, "SHOW DATABSES")
# CLOSE DB CONNECTION
dbDisconnect(con)
# CANCEL PORT FORWARDING
system("ssh -O cancel -L 3306:localhost:3306 me#jumphost.mycompany.com")

Setup ssh tunnel for mysql to mysql server that is accessed through ssh tunnel itself

There is a MySQL server running inside a VM(1) that do not have any port forwarding to public IP and can only be accessed inside the VMWare network. I have an ssh connection to a sibling VM(2) though which I can connect to the VM(1) and can also setup an ssh tunnel to it. How can I connect to MySQL on my local machine? I tried setting up a tunnel on VM(2) like this
ssh -fNTM -S $(dirname "$0")/tmp/$ENV-mysql-socket -L "$mysql":"$mysql_server" "$login"#"$ip" "$ENV.server"
But when I try to connect to it on my machine an error occurs:
handshake: reading initial communication packet

How to SSH Tunnel from MySQL server

I am trying to use an SSH tunnel to provide access from my server to the MySQL database on my home server, without using port forwarding on my home router. In other words, I want to run the MySQL server, and establish the SSH tunnel from my home server, and then be able to use the mysql client from the external server. But I keep getting:
bind: Address already in use
I run the mysql server:
service mysql start
and then describe the ssh tunnel:
ssh root#hf2.tr8.us -L 3306:127.0.0.1:3306 -N
or:
ssh root#hf2.tr8.us -L 3306:hf2.tr8.us:3306 -N
Both produce the address in use error. If I create the tunnel first, MySQL refuses to start.
How do I specify the SSH tunnel from the same server that I started MySQL?
What I was looking for was a "reverse" tunnel: -R. This works, from the server:
ssh root#hf2.tr8.us -R 3306:127.0.0.1:3306 -N

SSH Tunnel MySQL Connection with socket-connection via PhpStorm

By default, Database Manager from PhpStorm works well. But currently on a special Provider (1u1.de) I have some trouble to got this work.
I can connect to the Provider via SSH. If I want to connect to MySQL database, I have to use:
mysql --host=localhost --user=dbo123123123 -S /tmp/mysql5.sock --password='123123123';
That's works well via CLI on Server, but I didn't find a way to connect via PhpStorm to this Database.
For me it seems that the "socket-connection" may be the Problem. Does anybody have a clue how to got this to work?
Part of the Solution (?!):
Maybe a first part of an solution, I found that you be able to forwarding an Socket to your local pc as own socket this way:
ssh -nNT -L $(pwd)/yourLocal.sock:/var/run/mysqlREMOTEMYSQL.sock user#somehost
Source of Information
This show me, that the Socket is established:
netstat -ln | grep mysql
unix 2 [ ACC ] STREAM LISTENING 3713865 /myFolder/mysql5.sock
But I'm still unable to connect to this Socket with:
mysql -h localhost --protocol=SOCKET -u'username' -p'mypassword' -S /myFolder/mysql5.sock
Got this Error:
ERROR 2013 (HY000): Lost connection to MySQL server at 'reading initial communication packet', system error: 95 "Operation not supported"
ssh -L /tmp/mysql.sock:/var/run/mysqld/mysqld.sock sshuser#remotehost
and then
mysql -h localhost --protocol=SOCKET -u'username' -p'mypassword' -S /tmp/mysql.sock
seems to work fine for me
Use SSH to setup a port forward, this will allow you to connect securely to your database without exposing it to the world.
On ssh, use the -L argument to establish the tunnel.
ssh -L <local_port>:<remote_host>:<remote_port> user#host
This will open <local_port> on your local machine, and then redirect all packets out the other side of the tunnel, destened for the <remote_host>:<remote_port>
In your case, you might want to try something like this:
ssh -L 3306:127.0.0.1:3306 user#mybox.1u1.de
After establishing the tunnel, you will be able to connect to the database through a local port.
From your local machine, not the 1u1 host,
mysql -u <user> -p --host 127.0.0.1 --port 3306
If this works properly, you should be able to configure PhpStorm to use the same address, 127.0.0.1:3306
The SSH tunnel will need to remain open the entire time you need to be connected to the database.

can't access fortrabbit mysql db through terminal (ssh)

I can't access mysql through terminal on fortrabbit. I follow all steps but it is rejecting my password. However I can regullary login through ssh and edit my application. Anyone had that issue ? Thanks.
I have solved this in the past using a SSH tunnel. You open an SSH tunnel to the server, and then you connect to the MySQL server there from the endpoint of that tunnel. As such, to MySQL you appear to be connecting locally.
From the terminal:
First you need to open the tunnel, you can do it like this:
ssh -N -L8889:127.0.0.1:3306 username#your.fortrabbit.domain.com &
This opens port 8889, then opens a tunnel to your.fortrabbit.domain.com, then forwards that local port through the tunnel to the IP 127.0.0.1 and port 3306 relative to the server at your.fortrabbit.domain.com.
The options in more detail:
-N: Do not execute a remote command.
-L: Specifies the ports (local and remote).
8889: Your local port that is being forwarded.
127.0.0.1: the remote IP to which you're forwarding, relative to the server which ssh is connecting to
3306: the remote port to which you're forwarding.
username#your.fortrabbit.domain.com: Your username and domain with fortrabbit.
Now you're ready to open the connection. In the same terminal, use the following command:
mysql -h 127.0.0.1 -P 8889 -u mysql-username -p
port 8889 is now being forwarded to the port and IP of your MySQL server on the fortrabbit side, so just replace mysql-username with your username on the mysql server, and you're connected!
From a GUI:
You mentioned in your comments that you're using Ubuntu, so install MySQL Workbench from the Software Centre or here, create a New Connection and select the connection type as "Standard TCP/IP over SSH".
You will need to configure the following:
SSH Hostname: the hostname or IP of your ssh account with fortrabbit
SSH Username: your username with them
SSH Password: your password with them
SSH Keyfile: If you use keys for authentication, select the private one here.
MySQL Hostname: 127.0.0.1 (because it's local to the endpoint of your tunnel.
MySQL Server Port: normally "3306".
Username: The username for the DB
Password: The password for the DB
Default Schema: Whatever should be the default schema for this DB (can be left blank).
That should then connect from wherever you are!