Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
bash script for managing ssh tunnel and proxy settings for osx
#!/bin/bash
# Set me to your SSH server hostname or IP
host="example.com"
# Enable the Proxy location and open a tunnel if needed
if [ "$1" = "on" ]
then
# Check if there is an existing SSH connection...we don't want to go making
# unnecessary ones.
if [ -n "$(/bin/ps ax | /usr/bin/grep "ssh.$host" | /usr/bin/grep -v grep)" ]
then
echo "Connection open"
else
echo "Connection not open\n"
# If there is no connection open a tunnel
nohup /usr/bin/ssh -fN -C -D 8888 "$host" &
fi
# We switch to the Proxy location regardless of a new or pre-existing tunnel
networksetup -switchtolocation Proxy
# Switch to the Home location
elif [ "$1" = "off" ]
then
networksetup -switchtolocation Home
# Destory the tunnel connections...as well as all SSH connections, cause lazy
elif [ "$1" = "kill" ]
then
killall -9 ssh
# Show the status of the tunnel and location setting
elif [ "$1" = "status" ]
then
if [ -n "$(/bin/ps ax | /usr/bin/grep "ssh.*$host" | /usr/bin/grep -v grep)" ]
then
echo "Tunnel: open"
else
echo "Tunnel: closed"
fi
echo "Location: `networksetup -getcurrentlocation`"
else
# Helptext when no arg is supplied
echo "tunnel [on/off/kill]"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment