Last active
November 13, 2022 16:24
-
-
Save evokateur/0f213c895d8a650b4462cfda5952b810 to your computer and use it in GitHub Desktop.
lazy port forwarding
This file contains hidden or 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
#!/bin/bash | |
echo "Hello, footpad!" | |
me=`basename "$0"` | |
socket_path=~/"${me}".socket | |
if [ -z "$1" ] || [ "$1" == "open" ] | |
then | |
if [ -f "${socket_path}" ] | |
then | |
echo "Unlinking regular file ~/${me}.socket.." | |
unlink ${socket_path} | |
fi | |
if [ -S "${socket_path}" ] | |
then | |
news=$(ssh -S ${socket_path} -O check ${me} 2>&1) | |
good="Master running" | |
if [[ ! "$news" =~ ^${good}. ]] | |
then | |
echo "Removing dead ~/${me}.socket.." | |
rm -f ${socket_path} | |
fi | |
fi | |
if [ -S "${socket_path}" ] | |
then | |
echo "The ~/${me}.socket is already open." | |
else | |
echo "Opening ~/${me}.socket.." | |
ssh -M -S ~/${me}.socket -fnNT ${me} | |
echo "Done." | |
fi | |
else | |
if [ ! -S "${socket_path}" ] | |
then | |
echo "There is no ~/${me}.socket to ${1}." | |
elif [ "$1" == "exit" ] | |
then | |
news=$(ssh -S ${socket_path} -O check ${me} 2>&1) | |
good="Master running" | |
if [[ ! "$news" =~ ^${good}. ]] | |
then | |
echo "Removing dead ~/${me}.socket.." | |
rm -f ${socket_path} | |
else | |
echo "Exiting ~/${me}.socket.." | |
ssh -S ${socket_path} -O exit ${me} | |
fi | |
elif [ "$1" == "check" ] | |
then | |
echo "Checking ~/${me}.socket.." | |
ssh -S ${socket_path} -O check ${me} | |
else | |
echo "I don't know how to ${1} a ~/${me}.socket." | |
fi | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I keep a file around called
ssh-fp.sh
I symlink to it in my path with an arbitrary name, like
frobozz
The name of the symlink becomes the name of the socket (
~/frobozz.socket
) and is also the name of the host in~/.ssh/config
where the (actual) host name, user, and local forward(s) are configured:In everyday life it looks like:
If I need to port forward using a different server I make a new symlink and corresponding
~/.ssh.config
entry. Lazy.