Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@sryze
Last active September 3, 2023 10:20
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save sryze/1f77d803023b9586e6c565b634e10787 to your computer and use it in GitHub Desktop.
Save sryze/1f77d803023b9586e6c565b634e10787 to your computer and use it in GitHub Desktop.
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
Copy link

kymtwyf 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

@bassani2014
Copy link

Did you check to see if this works with the external usb ethernet adaptor that's listed above (USB 10/100/1000 LAN)? Assuming the web proxy is turned on for the usb ethernet adaptor, if i then perform the following in terminal:

networksetup -setwebproxystate "USB 10/100/1000 LAN" off

Then check the usb ethernet adaptor it doesn't turn off the web proxy. I'm running osx v12.6.3.

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