Skip to content

Instantly share code, notes, and snippets.

@lebedov
Created February 24, 2014 14:10
Show Gist options
  • Save lebedov/9189062 to your computer and use it in GitHub Desktop.
Save lebedov/9189062 to your computer and use it in GitHub Desktop.
Functions for managing ssh tunnels in bash
#!/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
}
@amcsi
Copy link

amcsi commented Mar 2, 2015

Thanks so much for this!

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