Created
February 24, 2014 14:10
-
-
Save lebedov/9189062 to your computer and use it in GitHub Desktop.
Functions for managing ssh tunnels in bash
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
#!/bin/bash | |
# Add these functions to your ~/.bashrc file. | |
function mktunnel { | |
if [[ $* == '' ]] || [[ $1 == '-h' ]]; then | |
echo 'Usage: mktunnel LOCALPORT REMOTEPORT REMOTEHOST' | |
else | |
ssh -fCNL $1:localhost:$2 $3; | |
fi | |
} | |
function lstunnel { | |
if [[ $1 == '-h' ]]; then | |
echo 'Usage: lstunnel [REGEX]' | |
else | |
count=1 | |
pgrep -fl "ssh -fCNL $*" | cut -f2- -d ' ' | while read LINE; do | |
echo $count: $LINE; | |
count=`expr $count + 1`; | |
done | |
fi | |
} | |
function rmtunnel { | |
if [[ $* == '' ]] || [[ $1 == '-h' ]]; then | |
echo 'Usage: rmtunnel N [REGEX]' | |
else | |
count=1 | |
pgrep -f "ssh -fCNL $2" | while read LINE; do | |
if [[ $1 == $count ]]; then | |
kill -9 $LINE; | |
break | |
fi | |
count=`expr $count + 1`; | |
done | |
fi | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks so much for this!