Skip to content

Instantly share code, notes, and snippets.

@sryze sryze/proxy
Last active Jun 22, 2019

Embed
What would you like to do?
Quickly toggle HTTP(S) proxy on Mac OS X from command line
#!/bin/sh
SERVICE="Ethernet" # or "Wi-Fi"
PROXY_HOST="127.0.0.1"
PROXY_PORT="8888"
while [[ $# > 0 ]]
do
case "$1" in
on)
networksetup -setwebproxystate $SERVICE on
networksetup -setwebproxy $SERVICE $PROXY_HOST $PROXY_PORT off
networksetup -setsecurewebproxystate $SERVICE on
networksetup -setsecurewebproxy $SERVICE $PROXY_HOST $PROXY_PORT off
echo 'Web proxy is on'
shift
;;
off)
networksetup -setwebproxystate $SERVICE off
networksetup -setsecurewebproxystate $SERVICE off
echo 'Web proxy is off'
shift
;;
esac
done
@kymtwyf

This comment has been minimized.

Copy link

commented May 7, 2019

Find the SERVICE using networksetup -listallnetworkservices command:

➜  sh networksetup -listallnetworkservices
An asterisk (*) denotes that a network service is disabled.
USB 10/100/1000 LAN
Wi-Fi
Bluetooth PAN
Thunderbolt Bridge

and I think it's safer to add quotes around $SERVICE in case the name contains spaces:

#!/bin/sh

SERVICE="Ethernet" # or "Wi-Fi"
PROXY_HOST="127.0.0.1"
PROXY_PORT="8888"

while [[ $# > 0 ]]
do
  case "$1" in
    on)
      networksetup -setwebproxystate "$SERVICE" on
      networksetup -setwebproxy "$SERVICE" $PROXY_HOST $PROXY_PORT off
      networksetup -setsecurewebproxystate "$SERVICE" on
      networksetup -setsecurewebproxy "$SERVICE" $PROXY_HOST $PROXY_PORT off
      echo 'Web proxy is on'
    shift
    ;;
    off)
      networksetup -setwebproxystate "$SERVICE" off
      networksetup -setsecurewebproxystate "$SERVICE" off
      echo 'Web proxy is off'
    shift
    ;;
  esac
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.