Skip to content

Instantly share code, notes, and snippets.

@timkuijsten
Created September 9, 2011 11:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timkuijsten/1206023 to your computer and use it in GitHub Desktop.
Save timkuijsten/1206023 to your computer and use it in GitHub Desktop.
os x ssh proxy
#!/bin/sh
#
# Copyright (c) 2011 Netsend
# Released under the MIT license.
#
###
# create a local ssh SOCKS proxy to RHOST and update OS X proxy settings.
# Tip: make your programs use the default OS X proxy.
#####
RHOST=shell.server.com
LPORT=9052
INTERACTIVE=1
echo=/bin/echo
usage() {
$echo "Setup an ssh proxy"
$echo ""
$echo "usage: `basename $0` [hostname]"
$echo ""
$echo "hostname remote ssh host"
$echo ""
}
PIDS=$(ps auxw | grep '[s]sh -2a' | awk '{ print $2}')
if [[ "$PIDS" != "" ]]; then
kill $PIDS
fi
if [ ! -z $1 ]; then
RHOST=$1
fi
set -e
ssh -2a -D127.0.0.1:$LPORT -NnqTxy $RHOST &
sleep 1
PID=$(lsof -tsTCP:LISTEN -i@127.0.0.1:$LPORT)
# suppress "/usr/local/bin/tunnel: line 50: kill: ($PID) - No such process"
trap 'kill $PID 2>/dev/null; sudo networksetup -setsocksfirewallproxystate Ethernet off; sudo networksetup -setsocksfirewallproxystate AirPort off; exit $?' INT TERM EXIT
# update any changes to the local proxy port
sudo networksetup -setsocksfirewallproxy Ethernet 127.0.0.1 $LPORT off
sudo networksetup -setsocksfirewallproxy AirPort 127.0.0.1 $LPORT off
# turn on the proxy
sudo networksetup -setsocksfirewallproxystate Ethernet on
sudo networksetup -setsocksfirewallproxystate AirPort on
# stay open if interactive
if [ "$INTERACTIVE" -eq 1 ]; then
echo PRESS CTRL-C to abort
cat
fi
@timkuijsten
Copy link
Author

tested on Snow Leopard, for Lion do s/AirPort/Wi-Fi/

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