Skip to content

Instantly share code, notes, and snippets.

@offlinehacker
Created August 9, 2011 22:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save offlinehacker/1135311 to your computer and use it in GitHub Desktop.
Save offlinehacker/1135311 to your computer and use it in GitHub Desktop.
Persistent ssh tunnel.
#!/bin/sh
if [ $# -ne 1 ]
then
echo "Syntax: `basename $0` username@domain.name"
exit
fi
IP=$1
cmd="ssh $IP -f -n /bin/ping -i 20 localhost"
program_exists() {
if pidof $1 > /dev/null; then
for p in $(pidof $1)
do
cmdline=$(cat /proc/"$p"/cmdline | xargs -0)
if [ "$cmdline" == "$cmd" ]; then
return 1
fi
done
fi
return 0
}
start() {
program_exists ssh
if [ $? -eq 0 ]; then
eval "$cmd > /dev/null"
fi
}
stop() {
if pidof ssh > /dev/null; then
for p in $(pidof ssh)
do
cmdline=$(cat /proc/"$p"/cmdline | xargs -0)
if [ "$cmdline" == "$cmd" ]; then
kill $p
fi
done
fi
}
start
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment