如何通过 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 21:25:29  来源:igfitidea点击:

How to set up a proxy via bash shell?

macosbashshellproxy

提问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 .profileor .bash_profileor 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 networksetupcommand. 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 -listnetworkserviceorderor networksetup -listallnetworkservices, then parse the list to get the name of the service you want.

如果您需要找出服务名称,请使用networksetup -listnetworkserviceordernetworksetup -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.000to your own server's IP and 00000to 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+Cto stop tunnelling.

./proxy(输入您的密码)开始,用于CTRL+C停止隧道。

Start it again and stop with CTRL+Cif 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 ./proxyagain.

如果您的管道破裂,请重新开始./proxy