Skip to content

Instantly share code, notes, and snippets.

@pjdevries
Created December 12, 2022 12:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pjdevries/9860a3d3c11021b95d6291c3ae4f35f0 to your computer and use it in GitHub Desktop.
Save pjdevries/9860a3d3c11021b95d6291c3ae4f35f0 to your computer and use it in GitHub Desktop.
Create ssh tunnel in shell script
remoteHost=xxx.xxx.xxx.xxx
remoteUser=uuuuuuuu
remotePort=XXXX
localPort=YYYY
function cleanup() {
ssh -S my-ctrl-socket -O exit ${remoteUser}@${remoteHost}
}
trap cleanup EXIT SIGHUP SIGINT SIGQUIT SIGABRT
# Create database tunnel.
ssh -S my-ctrl-socket -O exit ${remoteUser}@${remoteHost} >/dev/null 2>&1
ssh -4 -o ServerAliveInterval=5 -o ServerAliveCountMax=3 -M -S my-ctrl-socket -fnNT -L ${localPort}:localhost:{$remotePort} ${remoteUser}@${remoteHost} || exit $?
ssh -S my-ctrl-socket -O check ${remoteUser}@${remoteHost}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment