如何通过 bash shell 设置代理?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8666954/
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 set up a proxy via bash shell?
提问by Misha Moroshko
I set up a proxy on Mac via System Preferences -> Network -> Advanced -> Proxies
.
我通过System Preferences -> Network -> Advanced -> Proxies
.
Is that possible to do the same programatically via bash shell?
是否可以通过 bash shell 以编程方式执行相同的操作?
回答by maxHymanie
You can put this in your .profile
or .bash_profile
or run manually on a command line:
你可以把这个在你.profile
或.bash_profile
或命令行上手动运行:
export http_proxy=http://proxy.server.com:@aproxy:portnumber
export https_proxy=https://proxy.server.com:@aproxy:portnumber
It's also common to not use the proxy for the local connections
不将代理用于本地连接也很常见
export no_proxy=localhost,127.0.0.0/8,*.local
回答by Gordon Davisson
Yes, using the networksetup
command. There are separate options for setting different types of proxies (e.g. -setwebproxy
, -setsocksfirewallproxy
, etc), and you need to know the name of the primary network "service" (e.g. Ethernet, Airport... basically, the names listed in the sidebar of the Network preferences pane). Here's an example:
是的,使用networksetup
命令。有用于设置不同类型代理(例如-setwebproxy
、-setsocksfirewallproxy
等)的单独选项,并且您需要知道主要网络“服务”的名称(例如以太网、机场...首选项窗格)。下面是一个例子:
sudo networksetup -setwebproxy "Ethernet" myproxy.example.com 8000
If you need to figure out the service name, use networksetup -listnetworkserviceorder
or networksetup -listallnetworkservices
, then parse the list to get the name of the service you want.
如果您需要找出服务名称,请使用networksetup -listnetworkserviceorder
或networksetup -listallnetworkservices
,然后解析列表以获取所需服务的名称。
回答by line break
I use this script to proxy through my ssh server (not a web proxy).
我使用此脚本通过我的 ssh 服务器(而不是 Web 代理)进行代理。
#!/bin/bash
disable_proxy(){
sudo networksetup -setsocksfirewallproxystate Wi-Fi off
sudo networksetup -setsocksfirewallproxystate Ethernet off
echo "SOCKS proxy disabled."
}
trap disable_proxy INT
sudo networksetup -setsocksfirewallproxy Wi-Fi 127.0.0.1 9999
sudo networksetup -setsocksfirewallproxy Ethernet 127.0.0.1 9999
sudo networksetup -setsocksfirewallproxystate Wi-Fi on
sudo networksetup -setsocksfirewallproxystate Ethernet on
echo "SOCKS proxy enabled."
echo "Tunneling..."
ssh -ND 9999 000.000.000.000 -p 00000
Change 000.000.000.000
to your own server's IP and 00000
to your own port and you should be able to reuse it with your own ssh server. You can save this script in your home directory named say proxy
.
更改000.000.000.000
为您自己的服务器的 IP 和00000
您自己的端口,您应该可以在您自己的 ssh 服务器上重复使用它。您可以将此脚本保存在名为 say 的主目录中proxy
。
Start it with ./proxy
(type your password), use CTRL+C
to stop tunnelling.
以./proxy
(输入您的密码)开始,用于CTRL+C
停止隧道。
Start it again and stop with CTRL+C
if you forgot to stop tunnelling and next day you are wondering why your internet connection is down.
CTRL+C
如果您忘记停止隧道,请重新启动并停止,第二天您想知道为什么您的互联网连接已关闭。
If you get a broken pipe, just start ./proxy
again.
如果您的管道破裂,请重新开始./proxy
。