how to you toggle on and off a web proxy in os x from the command line

前端 未结 7 1367
予麋鹿
予麋鹿 2021-01-31 15:45

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 a

7条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-31 16:35

    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
    

提交回复
热议问题