Created
December 12, 2022 12:46
-
-
Save pjdevries/9860a3d3c11021b95d6291c3ae4f35f0 to your computer and use it in GitHub Desktop.
Create ssh tunnel in shell script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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