脚本内容
创建文件 ~/.proxy-setup.sh
:
#!/bin/bash
# 使用固定的 Windows IP
HOST_IP="192.168.3." # 替换为你的 Windows IP
PORT=7890 # 替换为你的代理端口
function set_proxy() {
export http_proxy="http://${HOST_IP}:${PORT}"
export https_proxy="http://${HOST_IP}:${PORT}"
export ALL_PROXY="http://${HOST_IP}:${PORT}"
echo "Proxy enabled:"
echo "http_proxy: $http_proxy"
echo "https_proxy: $https_proxy"
echo "ALL_PROXY: $ALL_PROXY"
# 测试连接
echo -e "\nTesting proxy connection..."
curl -I https://www.google.com 2>/dev/null | head -n 1 || echo "Proxy connection failed. Please check your settings."
}
function unset_proxy() {
unset http_proxy
unset https_proxy
unset ALL_PROXY
echo "Proxy disabled"
}
# 根据参数执行操作
case "$1" in
"on")
set_proxy
;;
"off")
unset_proxy
;;
*)
set_proxy # 默认开启代理
;;
esac
使用方法
- 配置脚本:
# 创建脚本
nano ~/.proxy-setup.sh
# 添加执行权限
chmod +x ~/.proxy-setup.sh
- 使用代理:
# 开启代理
source ~/.proxy-setup.sh on
# 关闭代理
source ~/.proxy-setup.sh off
可选配置
如果想要更方便地使用,可以在 ~/.bashrc
中添加别名:
echo 'alias proxy="source ~/.proxy-setup.sh"' >> ~/.bashrc
source ~/.bashrc
之后就可以使用 proxy on
和 proxy off
来控制代理了。
注意事项
- 需要修改脚本中的
HOST_IP
为你的 Windows IP 地址 - 修改
PORT
为你的代理客户端端口 - Windows 代理客户端需要开启"允许来自局域网的连接"选项
- 代理设置只对当前终端会话有效
评论区