how to you toggle on and off a web proxy in os x f

2020-05-19 02:46发布

问题:

In OS X, you turn on and off a web proxy from System Preferences > Network > Proxies, by checking Web Proxy (HTTP) and designating the Web Proxy Server etc. and by clicking OK and then "Apply". This is way too many steps. Is there a way to do this all from the command line and a shell script?

回答1:

For an unauthenticated proxy (and assuming it's the Ethernet service you want to configure):

networksetup -setwebproxy Ethernet proxy.example.net 80 off

for authenticated:

networksetup -setwebproxy Ethernet proxy.example.net 80 on proxyuser "p4ssw0rd"

and to turn it off:

networksetup -setwebproxystate Ethernet off

If the network service isn't named just "Ethernet", you may need to parse networksetup -listallnetworkservices or -listnetworkserviceorder to get the correct name.



回答2:

To just toggle on/off my proxies in OSX Mavericks, I set up this script. Note that this example just affects my wi-fi adapter. I am toggling on/off the web, streaming, and SOCKS proxies all at once. You could set the proxy address as well, per Gordon's example, but I already had this saved through the System Preferences > Network > Proxies GUI.

BASH Script, saved as prox.sh:

#!/bin/bash

e=$(networksetup -getwebproxy wi-fi | grep "No")

if [ -n "$e" ]; then
  echo "Turning on proxy"
  sudo networksetup -setstreamingproxystate wi-fi on
  sudo networksetup -setsocksfirewallproxystate wi-fi on
  sudo networksetup -setwebproxystate wi-fi on
else
  echo "Turning off proxy"
  sudo networksetup -setstreamingproxystate wi-fi off
  sudo networksetup -setsocksfirewallproxystate wi-fi off
  sudo networksetup -setwebproxystate wi-fi off
fi

Then symlink the script on the command line:

ln -s /Script/Location/prox.sh prox-toggle

Now you can toggle the proxies on/off at the command line:

bash prox-toggle


回答3:

I prepared a script named proxy that might help,

#!/bin/bash
#
#
#   Author: Md. Sazzad Hissain Khan
#   Date:   8 July, 2017
#
#

NETWORK_SERVICE_NAME="Ethernet"

if [ "$#" -ne 1 ]; then
    echo "Argument missing [on/off]"
    exit 0
fi

if [ $1 == "on" ]; then
    echo "Enabling secure proxy for $NETWORK_SERVICE_NAME"
    networksetup -setsecurewebproxystate "$NETWORK_SERVICE_NAME" on
elif [ $1 == "off" ]; then
    echo "Disabling secure proxy for $NETWORK_SERVICE_NAME"
    networksetup -setsecurewebproxystate "$NETWORK_SERVICE_NAME" off
else
    echo "Argument invalid [permitted:on/off]"
fi

NETWORK_SERVICE_NAME is the name of your active network which you need to configure.

  1. Create proxy file in /usr/local/bin.
  2. Copy above script into proxy.
  3. Set executable permission for the file using sudo chmod 777 proxy .

How to use:

proxy on
proxy off


回答4:

Here is an Applescript that turns on and off the proxy at macworld.

  • http://hints.macworld.com/article.php?story=2003101617122867


回答5:

Just the Toggling :)

networksetup -setwebproxystate <networkservice> <on off>
networksetup -setsecurewebproxystate <networkservice> <on off>

Example :

networksetup -setwebproxystate  Wi-Fi on
networksetup -setsecurewebproxystate  Wi-Fi on

To handle the Modification alert : prefix sudo like

sudo networksetup -setwebproxystate  Wi-Fi on
sudo networksetup -setsecurewebproxystate  Wi-Fi on


回答6:

Enabling and Disabling Proxy with a keyboard shortcut

In terminal, you can turn wifi proxy off and on with these commands

networksetup -setwebproxystate Wi-Fi <on | off>
networksetup -setsecurewebproxystate Wi-Fi <on | off>

and Ethernet

networksetup -setwebproxystate Ethernet <on | off>
networksetup -setsecurewebproxystate Ethernet <on | off>

Here's a one-liner to toggle between on and off (Using Wi-Fi example)

e=$(networksetup -getwebproxy wi-fi | grep "No")

if [ -n "$e" ]; then
  networksetup -setwebproxystate  Wi-Fi on
  networksetup -setsecurewebproxystate  Wi-Fi on
else
  networksetup -setwebproxystate  Wi-Fi off
  networksetup -setsecurewebproxystate  Wi-Fi off
fi

Create a keyboard shortcut that runs a shell command

  1. Start Automator, and create a new Service.

  2. Set "Service receives selected: to "no input" in "any application".

  3. Add an action named "Run Shell Script". It's in the Utilities section of the Actions Library.

  4. Insert the bash command you want into the text box and test run it using the Run button (top right). It should do whatever the script does (off, on or toggle), and there should be green ticks below the Action.

  5. Save it, giving it a service name you can remember.

  6. Go to System Preferences -> Keyboard, and go to the Shortcuts tab

  7. Go to the Services section, and scroll down to General - you should find your service there. If you select the line, you can click "add shortcut" and give it a keyboard shortcut.



回答7:

As I needed a simple script that will just toggle both HTTP and HTTPS proxies on/off at the same time, here it is:

#!/usr/bin/env bash
# Toggles *both* HTTP and HTTP proxy for a preconfigured service name ("Wi-Fi" or "Ethernet").

NETWORK_SERVICE_NAME="Wi-Fi" # Wi-Fi | Ethernet

IS_PROXY_ENABLED=$(networksetup -getwebproxy "$NETWORK_SERVICE_NAME" | head -n 1 | grep Yes)

if [ -z "$IS_PROXY_ENABLED" ]; then
    echo "Enabling HTTP and HTTPs proxy for $NETWORK_SERVICE_NAME"
    networksetup -setstreamingproxystate "$NETWORK_SERVICE_NAME" on
    networksetup -setwebproxystate "$NETWORK_SERVICE_NAME" on
    networksetup -setsecurewebproxystate "$NETWORK_SERVICE_NAME" on
else
    echo "Disabling HTTP and HTTPs proxy for $NETWORK_SERVICE_NAME"
    networksetup -setstreamingproxystate "$NETWORK_SERVICE_NAME" off
    networksetup -setwebproxystate "$NETWORK_SERVICE_NAME" off
    networksetup -setsecurewebproxystate "$NETWORK_SERVICE_NAME" off
fi