Skip to content

Instantly share code, notes, and snippets.

@kjhealy
Created February 7, 2013 14:03
Show Gist options
  • Save kjhealy/4731093 to your computer and use it in GitHub Desktop.
Save kjhealy/4731093 to your computer and use it in GitHub Desktop.
Set up an SSH SOCKS proxy from the command line.
#!/bin/bash
## (C) George Goulas, 2011
##
## Proxy service configuration script for OSX
##
## SETTINGS
##
# SOCKS PROXY PORT
PORT=8080
# SSH OPTIONS TO CREATE PROXY
SSH_OPTS="-C2qTnNfD"
# user@host
SSH_HOST="[email protected]"
# SSH PORT
SSH_PORT=22
# OSX network service to configure proxy for
NET_SERVICE="Ethernet" # or "Wi-Fi". use "networksetup -listallnetworkservices" to see available services
# Verbose, if not empty, it prints diagnosing messages
VERBOSE=1
##
## END OF SETTINGS, DO NOT MODIFY PAST THIS POINT
##
SSH_CMD="ssh ${SSH_OPTS} ${PORT} -p ${SSH_PORT} ${SSH_HOST}"
function report {
MSG=$1
if [ -n "${VERBOSE}" ]; then
echo $MSG
fi
}
function enableProxy {
networksetup -setsocksfirewallproxy ${NET_SERVICE} localhost ${PORT}
networksetup -setsocksfirewallproxystate ${NET_SERVICE} on
${SSH_CMD}
}
function disableProxy {
ps -ax | grep "${SSH_CMD}" | grep -v grep | awk '{print $1}'| xargs kill
networksetup -setsocksfirewallproxystate ${NET_SERVICE} off
}
function showStatus {
ps -ax | grep "${SSH_CMD}" | grep -v grep > /dev/null
if [ $? -eq 0 ]; then
echo SSH SOCKS Proxy status: ON
else
echo SSH SOCKS Proxy status: OFF
fi
networksetup -getsocksfirewallproxy ${NET_SERVICE} | grep Enabled | grep Yes > /dev/null
if [ $? -eq 0 ]; then
echo Proxy setting in network setup for ${NET_SERVICE}: ON
else
echo Proxy setting in network setup for ${NET_SERVICE}: OFF
fi
}
case "$1" in
on) report "Enabling Proxy"
enableProxy
;;
off) report "Disabling Proxy"
disableProxy
;;
status) echo status
showStatus
;;
*) echo Options: on to enable proxy, off to disable, status to see status.
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment