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

前端 未结 7 1366
予麋鹿
予麋鹿 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

    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 
    networksetup -setsecurewebproxystate Wi-Fi 
    

    and Ethernet

    networksetup -setwebproxystate Ethernet 
    networksetup -setsecurewebproxystate Ethernet 
    

    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.

提交回复
热议问题