Skip to content

Instantly share code, notes, and snippets.

@evokateur
Last active Sep 11, 2021
Embed
What would you like to do?
lazy port forwarding
#!/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
@evokateur
Copy link
Author

evokateur commented Jul 19, 2021

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:

Host frobozz
    HostName server
    User user
    LocalForward 33184 10.1.10.184:3389
    LocalForward 33121 10.1.10.121.121:3389
    LocalForward 33067 10.1.10.67:3389

In everyday life it looks like:

√ ~ $ frobozz check
Hello, footpad!
There is no ~/frobozz.socket to check.

√ ~ $ frobozz
Hello, footpad!
Opening ~/frobozz.socket..
Done.

√ ~ $ frobozz check
Hello, footpad!
Checking ~/frobozz.socket..
Master running (pid=14049)

√ ~ $ frobozz boop
Hello, footpad!
I don't know how to boop a ~/frobozz.socket.

√ ~ $ frobozz exit
Hello, footpad!
Exiting ~/frobozz.socket..
Exit request sent.

If I need to port forward using a different server I make a new symlink and corresponding ~/.ssh.config entry. Lazy.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment