编写脚本,快速开启与关闭代理。
前置条件
指定网卡
- 若在虚拟机
Debian12
中通过代理上网,则在需要代理服务器(或科学上网软件)的Setting-Outbound-Interface Name
中,选择需要使用的网卡,通常VMware Workstation Pro
中为VMnet8
。 - 若在主系统中(运行科学上网软件的系统)通过代理上网,则直接指定主系统的实际物理网卡(例如
WiFi
)即可。
Debian系统设置
- 可以使用
curl ipinfo.io
查看当前互联网出口IP
详情:
rz@debase:~$ curl ipinfo.io
{
"ip": "117.63.39.243",
"city": "Shanghai",
"region": "Shanghai",
"country": "CN",
"loc": "31.2222,121.4581",
"org": "AS4134 CHINANET-BACKBONE",
"postal": "200000",
"timezone": "Asia/Shanghai",
"readme": "https://ipinfo.io/missingauth"
}rz@debase:~$
当前互联网出口为上海
- 查看主系统的
IP
地址(主系统VMnet8
的IP
地址),Windows 11
中可以使用ipconfig
命令行命令显示IP
地址:
以太网适配器 VMware Network Adapter VMnet8:
连接特定的 DNS 后缀 . . . . . . . :
...
IPv4 地址 . . . . . . . . . . . . : 192.168.152.1
子网掩码 . . . . . . . . . . . . : 255.255.255.0
默认网关. . . . . . . . . . . . . :
- 临时性配置代理:
rz@debase:~$ export ALL_PROXY=socks5://192.168.152.1:7890
rz@debase:~$ curl ipinfo.io
{
"ip": "103.172.41.199",
"city": "Hong Kong",
"region": "Hong Kong",
"country": "HK",
"loc": "22.2783,114.1747",
"org": "AS38136 Akari Networks",
"postal": "999077",
"timezone": "Asia/Hong_Kong",
"readme": "https://ipinfo.io/missingauth"
}rz@debase:~$
- 其中
socks5://192.168.152.1:7890
的IP
地址为主系统的IP
地址;7890
为科学上网软件配置的Port
,或直接使用代理服务。- 需要开启科学上网软件的
Global
代理,同时开启科学上网软件的Allow LAN
。
- 取消临时代理可以使用,
unset
命令:
rz@debase:~$unset ALL_PROXYY
rz@debase:~$ curl ipinfo.io
{
"ip": "117.63.39.243",
"city": "Shanghai",
"region": "Shanghai",
"country": "CN",
"loc": "31.2222,121.4581",
"org": "AS4134 CHINANET-BACKBONE",
"postal": "200000",
"timezone": "Asia/Shanghai",
"readme": "https://ipinfo.io/missingauth"
}rz@debase:~$
- 编写脚本快速设置代理服务。在家目录中创建
proxy
文件夹,其中创建文件proxyon
,其文件内容如下:
IP=192.168.152.1
export HTTP_PROXY="http://$IP:7890"
export HTTPS_PROXY="http://$IP:7890"
export NO_PROXY="localhost,127.0.0.1,192.168.152.0/24"
注意,不要写
socket5
,例如如下形式,对于nerdctl
等工具是不支持的。
IP=192.168.152.1
export ALL_PROXY="socks5://$IP:7890"
- 在
proxy
文件夹中创建文件proxyoff
,其文件内容如下:
unset HTTP_PROXY
unset HTTPS_PROXY
unset NO_PROXY
- 在主系统科学上网软件开启的状态下,使用下列命令可以快捷开启与关闭代理:
rz@debase:~/proxy$ ll
total 8
-rw-r--r-- 1 rz rz 50 Dec 26 13:03 proxyoff
-rw-r--r-- 1 rz rz 145 Dec 26 13:03 proxyon
rz@debase:~/proxy$ . proxyon
rz@debase:~/proxy$ curl https://ipinfo.io
{
"ip": "103.172.41.199",
"city": "Hong Kong",
"region": "Hong Kong",
"country": "HK",
"loc": "22.2783,114.1747",
"org": "AS38136 Akari Networks",
"postal": "999077",
"timezone": "Asia/Hong_Kong",
"readme": "https://ipinfo.io/missingauth"
}rz@debase:~/proxy$ . proxyoff
rz@debase:~/proxy$ curl https://ipinfo.io
{
"ip": "117.63.39.243",
"city": "Shanghai",
"region": "Shanghai",
"country": "CN",
"loc": "31.2222,121.4581",
"org": "AS4134 CHINANET-BACKBONE",
"postal": "200000",
"timezone": "Asia/Shanghai",
"readme": "https://ipinfo.io/missingauth"
}rz@debase:~/proxy$
强调,需要使用
curl https://ipinfo.io
验证,不要直接使用curl ipinfo.io
,在一些场景下(例如nerdctl
代理配置中)易于误导结果。
- 可以使用
export -p
打印当前会话Shell
中所有环境变量:
rz@debase:~/proxy$ cd
rz@debase:~$ export -p
declare -x DBUS_SESSION_BUS_ADDRESS="unix:path=/run/user/1000/bus"
declare -x DISPLAY="localhost:10.0"
declare -x HISTTIMEFORMAT="%F %T "
declare -x HOME="/home/rz"
declare -x LANG="en_US.UTF-8"
declare -x LOGNAME="rz"
...
env
命令可以同样输出:
rz@debase:~$ env
SHELL=/bin/bash
HISTTIMEFORMAT=%F %T
PWD=/home/rz
LOGNAME=rz
XDG_SESSION_TYPE=tty
MOTD_SHOWN=pam
HOME=/home/rz
...