question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Setup and teardown an SSH tunnel

See original GitHub issue

I have some databases that are only accessible via SSH tunnel. Right now, I have to start a tunnel in another tab or the background, then use pgcli to connect to the tunnel locally. When I’m done, I have to remember to kill the tunnel. It would be great if I could pass another argument(s) to pgcli and it would setup and teardown the tunnel for me.

I imagine something like pgcli --ssh 'ssh_user:ssh_password@ssh_host:ssh_port' 'postgresql://pg_user:pg_password@pg_host:pg_port/dbname' which would do the equivalent of:

ssh -p ssh_port -L localhost:0000:pg_host:pg_port -N ssh_user@ssh_host
psql -U pg_user -h localhost -p 0000 -d dbname

where 0000 is some random available port.

sshtunnel looks like it might be well suited to manage the tunnel. It also appears to respect settings in ssh_config(5), so pgcli users with more complicated SSH needs can configure them there.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:33
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

10reactions
tsrotencommented, Oct 16, 2017

@cbandy @diddeb You can also connect via SSH without worrying about closing the tunnel via the ExitOnForwardFailure configuration option (-o). And, you can make SSH go in the background so you don’t need two tabs (-f).

I have a shell function I can call whenever I want to bind a local port to a postgres port on the SSH server. The sleep 10 means that I’ll have 10 seconds to connect to the port before the SSH connection closes automatically.

sshb_pg() {
    ssh -f -o ExitOnForwardFailure=yes -L 9432:localhost:5432 \
    $1 sleep 10
}

Usage:

$ sshb_pg ssh_user@ssh_host && pgcli [...]

Use shell aliases to do it all at once:

alias pg_ssh="sshb_pg ssh_user@ssh_host && pgcli -U pg_user -h localhost -p 9432 -d dbname"

$ pg_ssh
dbname> 
9reactions
amjithcommented, Feb 7, 2016

Thank you for the request. I’m not sure if there is enough interest for this to be built into pgcli as opposed to keeping them separate.

I’m hesitant to build this in as this has potential to cause a lot more support load. People might have different versions of paramiko or pycrypto or openssl lib etc and debugging the issues raising from that can be quite hairy.

I can keep this issue open and if I see more people interested in this, I’ll reconsider.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Set up SSH Tunneling (Port Forwarding)
Set up SSH Tunneling in Windows # · Launch Putty and enter the SSH server IP Address in the Host name (or IP...
Read more >
SSH Tunneling Explained
SSH tunneling is a method to transport additional data streams within an existing SSH session. This post explains different SSH Tunneling ...
Read more >
How To Set Up SSH Tunneling on a VPS
Choose the connection and load the settings. · Go to Connection->SSH->Tunnels · Set it like that: It looks like that · Click on...
Read more >
How to Create SSH Tunneling or Port Forwarding in Linux
In this article, we will demonstrate how to quickly and easily setup a SSH tunneling or the different types of port forwarding in...
Read more >
What is an SSH Tunnel & SSH Tunneling?
This SSH connection is set up with an option that enables TCP port forwarding from a port on the external server to an...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found