Skip to content

Instantly share code, notes, and snippets.

@kevinmilner
Last active May 6, 2020 19:04
Show Gist options
  • Save kevinmilner/3cb731bf167710ea46a488cb0c2c47b3 to your computer and use it in GitHub Desktop.
Save kevinmilner/3cb731bf167710ea46a488cb0c2c47b3 to your computer and use it in GitHub Desktop.
SSH tunnel management .bashrc functions
FWD_CMD="ssh -f -N -o Compression=no -L $LOCAL_PORT:$REMOTE_HOST:$REMOTE_PORT $FWD_USER@$FWD_HOST"
function sshpid {
SSH_PID=$(ps aux | grep "$FWD_CMD" | grep -v 'grep' | awk '{print $2}')
}
function fwdrunning {
sshpid
if [[ -n $SSH_PID ]];then
echo "ssh tunnel is running with PID $SSH_PID"
else
echo "ssh tunnel not found"
fi
}
function sshfwd {
sshpid
if [[ -n $SSH_PID ]];then
echo "ssh tunnel already running with PID $SSH_PID"
else
echo "Establishing SSH tunnel"
$FWD_CMD
RET=$?
if [[ $RET -eq 0 ]];then
sshpid
echo "successfully started tunnel with PID $SSH_PID, kill with killsshfwd"
else
echo "SSH tunnel exited with status $RET"
fi
fi
}
function killsshfwd {
sshpid
if [[ -n $SSH_PID ]];then
echo "killing SSH with PID $SSH_PID"
kill $SSH_PID
else
echo "tunnel not running"
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment