Ssh will still wait the end of tunnel usage before ending, so the short delay is not an issue: ssh -o ExitOnForwardFailure=yes -f -L port:host:5432 -i ~/.ssh/key. That way there won't be a long lived idle ssh command that has to be searched if it has to be killed. It's also possible to replace -N with a short remote sleep command. An SSH client connects to a Secure Shell server, which allows you to run terminal commands as if you were sitting in front of another computer. That way running the function multiple times, won't leave (multiple-1) useless ssh running. Putting all this together, replace the ssh line in the function with: ssh -o ExitOnForwardFailure=yes -f -N -L port:host:5432 -i ~/.ssh/key.pub To be successfully established before placing itself in the If the ExitOnForwardFailure configuration option is set to “yes”, then a client started with -f will wait for all remote port forwards Recommended way to start X11 programs at a remote site is with This is useful if ssh is going to ask for passwords or passphrases,īut the user wants it in the background. Requests ssh to go to background just before command execution. Just don't use & but use instead the option -f: The first command didn't have time to establish a tunnel when the second command was run, thus giving a "Connection refused". i connect to the db just fine! PGPASSWORD=password psql dbname -U user -h ip_address -p port Is the server running on host "127.0.0.1" and acceptingĪlthough the initial command is running in the background: ps aux | grep host Psql: could not connect to server: Connection refused PGPASSWORD=password psql dbname -U user -h ip_address -p port Enter 3306 in the Source Port field, and 127.0.0. But nothing worked with me:Īttempt 1: run without background stuff: function db() Perform the following steps to create an SSH tunnel to the MySQL server with PuTTY: Launch Putty and enter the IP Address of the server in the Host name (or IP address) field: Under the Connection menu, expand SSH and select Tunnels. We will run command ssh-keygen: ssh-keygen -t rsa. Which is awesome, but I would like to put both these in a single function. For making tunnel via SSH we need to form public and private key, that help to authorize user more securely and automate process. I run these two commands all the time to connect to my rds instance on aws that's protected behind a firewall (so i tunnel through the ec2 instance) like so:Ĭommand 1: open the tunnel (run on background) ssh -N -L port:host:5432 -i ~/.ssh/key.pub &Ĭommand 2: connect to db through tunnel port: PGPASSWORD=password psql dbname -U user -h ip_address -p port
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |