Skip to content

Instantly share code, notes, and snippets.

@mattberjon
Created November 27, 2017 21:32
Show Gist options
  • Save mattberjon/b69df0bcb91aad2a7592a6b5552c3510 to your computer and use it in GitHub Desktop.
Save mattberjon/b69df0bcb91aad2a7592a6b5552c3510 to your computer and use it in GitHub Desktop.
Create or kill an SSH tunnel
#!/bin/sh
# ~/bin/sshr
#
# modified: 26 Jan 2012
# author: matthieu berjon <matthieu.berjon@wavefield.fr>
# licence: WTFPL
#
# description: create or kill a remote ssh tunnel
LOGIN=
SERVER=
connect_tunnel () {
ssh -C2TnN -D 8080 $LOGIN@$SERVER &
}
kill_tunnel () {
KILL=$(ps aux | grep "ssh -C2TnN -D" | head -n 1 | awk '{print $2}')
kill -9 $KILL
}
help ()
{
cat << EOF
Use: $0 [options] [file]
Options:
-c, --connect connect to the tunnel
-k, --kill Stop the tunnel
-h, --help Show this help
EOF
}
while getopts ckh OPTION
do
case $OPTION in
c | --connect)
connect_tunnel $OPTARG
;;
k | --kill)
kill_tunnel
;;
h | --help)
help
;;
esac
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment