macos 如何从命令行打开和关闭 os x 中的 Web 代理
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4029471/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to you toggle on and off a web proxy in os x from the command line
提问by dan
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?
在 OS X 中,您可以从“系统偏好设置”>“网络”>“代理”打开和关闭 Web 代理,方法是选中 Web 代理 (HTTP) 并指定 Web 代理服务器等,然后单击“确定”,然后单击“应用”。这个步骤太多了。有没有办法从命令行和 shell 脚本完成这一切?
回答by Gordon Davisson
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.
如果网络服务不只是命名为“以太网”,您可能需要解析networksetup -listallnetworkservices
或-listnetworkserviceorder
获取正确的名称。
回答by Eric
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.
为了在 OSX Mavericks 中打开/关闭我的代理,我设置了这个脚本。请注意,此示例仅影响我的 wi-fi 适配器。我同时打开/关闭网络、流媒体和 SOCKS 代理。您也可以按照Gordon 的示例设置代理地址,但我已经通过系统偏好设置 > 网络 > 代理 GUI 保存了这个地址。
BASH Script, saved as prox.sh:
BASH 脚本,另存为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
回答by Sazzad Hissain Khan
I prepared a script named proxy that might help,
我准备了一个名为 proxy 的脚本,可能会有所帮助,
#!/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 [ == "on" ]; then
echo "Enabling secure proxy for $NETWORK_SERVICE_NAME"
networksetup -setsecurewebproxystate "$NETWORK_SERVICE_NAME" on
elif [ == "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.
NETWORK_SERVICE_NAME
是您需要配置的活动网络的名称。
- Create
proxy
file in/usr/local/bin.
- Copy above script into
proxy.
- Set executable permission for the file using
sudo chmod 777 proxy
.
- 在中创建
proxy
文件/usr/local/bin.
- 将上面的脚本复制到
proxy.
- 使用 设置文件的可执行权限
sudo chmod 777 proxy
。
How to use:
如何使用:
proxy on
proxy off
回答by Abhishek Bedi
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
如
sudo networksetup -setwebproxystate Wi-Fi on
sudo networksetup -setsecurewebproxystate Wi-Fi on
回答by pyfunc
Here is an Applescript that turns on and off the proxy at macworld.
这是一个在 macworld 上打开和关闭代理的 Applescript。
回答by Rich
Enabling and Disabling Proxy with a keyboard shortcut
使用键盘快捷键启用和禁用代理
In terminal, you can turn wifi proxy off and on with these commands
在终端中,您可以使用这些命令关闭和打开 wifi 代理
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)
这是一个可以在开和关之间切换的单线(使用 Wi-Fi 示例)
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
创建一个运行 shell 命令的键盘快捷键
Start Automator, and create a new Service.
Set "Service receives selected: to "no input" in "any application".
Add an action named "Run Shell Script". It's in the Utilities section of the Actions Library.
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.
Save it, giving it a service name you can remember.
Go to System Preferences -> Keyboard, and go to the Shortcuts tab
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.
启动 Automator,并创建一个新服务。
在“任何应用程序”中将“服务接收选择:”设置为“无输入”。
添加名为“运行 Shell 脚本”的操作。它位于操作库的实用程序部分。
将所需的 bash 命令插入文本框中,然后使用“运行”按钮(右上角)测试运行它。它应该执行脚本执行的任何操作(关闭、打开或切换),并且操作下方应该有绿色勾号。
保存它,给它一个你能记住的服务名称。
转到系统偏好设置 -> 键盘,然后转到快捷方式选项卡
转到“服务”部分,然后向下滚动到“常规”——您应该在那里找到您的服务。如果选择该行,则可以单击“添加快捷方式”并为其指定键盘快捷键。
回答by syntagma
As I needed a simple script that will just toggle both HTTP and HTTPS proxies on/off at the same time, here it is:
因为我需要一个简单的脚本来同时打开/关闭 HTTP 和 HTTPS 代理,这里是:
#!/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