本文目录导读:

是的,实用脚本可以自动配置代理服务器,这在网络管理、开发测试、隐私保护等场景中非常常见,根据你的操作系统和需求,可以通过 Bash(Linux/macOS)、PowerShell(Windows) 或 批处理脚本 来实现。
以下是几种常见场景的脚本示例和配置方法:
Linux/macOS 系统(Bash 脚本)
在 Linux 或 macOS 终端中,通过设置环境变量来临时或永久配置代理。
临时配置(仅当前终端会话有效)
#!/bin/bash # 临时设置 HTTP 和 HTTPS 代理 export http_proxy="http://127.0.0.1:7890" export https_proxy="http://127.0.0.1:7890" export HTTP_PROXY="http://127.0.0.1:7890" export HTTPS_PROXY="http://127.0.0.1:7890" echo "代理已设置:http://127.0.0.1:7890"
永久配置(写入 shell 配置文件)
#!/bin/bash # 将代理设置写入 ~/.bashrc 或 ~/.zshrc echo 'export http_proxy="http://127.0.0.1:7890"' >> ~/.bashrc echo 'export https_proxy="http://127.0.0.1:7890"' >> ~/.bashrc source ~/.bashrc # 立即生效 echo "代理配置已写入并生效"
切换代理的实用脚本(支持开启/关闭)
#!/bin/bash
# 代理配置脚本:proxy_toggle.sh
PROXY_HOST="127.0.0.1"
PROXY_PORT="7890"
case "$1" in
on)
export http_proxy="http://$PROXY_HOST:$PROXY_PORT"
export https_proxy="http://$PROXY_HOST:$PROXY_PORT"
echo "代理已开启:http://$PROXY_HOST:$PROXY_PORT"
;;
off)
unset http_proxy https_proxy HTTP_PROXY HTTPS_PROXY
echo "代理已关闭"
;;
status)
echo "http_proxy: $http_proxy"
echo "https_proxy: $https_proxy"
;;
*)
echo "用法: source proxy_toggle.sh {on|off|status}"
;;
esac
注意:要使其影响当前 shell,需要使用 source proxy_toggle.sh on 执行。
Windows 系统(PowerShell 脚本)
Windows 通过系统代理设置实现全局代理。
设置系统代理
# 临时设置系统代理 $proxy = "127.0.0.1:7890" Set-ItemProperty -Path "HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings" -Name ProxyServer -Value $proxy Set-ItemProperty -Path "HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings" -Name ProxyEnable -Value 1 Write-Host "代理已开启:$proxy"
关闭系统代理
Set-ItemProperty -Path "HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings" -Name ProxyEnable -Value 0 Write-Host "代理已关闭"
完整的开关脚本
# proxy_switch.ps1
param([string]$action="status")
$proxy = "127.0.0.1:7890"
$registryPath = "HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings"
switch ($action) {
"on" {
Set-ItemProperty -Path $registryPath -Name ProxyServer -Value $proxy
Set-ItemProperty -Path $registryPath -Name ProxyEnable -Value 1
Write-Host "代理已开启:$proxy"
}
"off" {
Set-ItemProperty -Path $registryPath -Name ProxyEnable -Value 0
Write-Host "代理已关闭"
}
"status" {
$enabled = (Get-ItemProperty -Path $registryPath).ProxyEnable
$server = (Get-ItemProperty -Path $registryPath).ProxyServer
if ($enabled -eq 1) {
Write-Host "代理状态:开启,服务器:$server"
} else {
Write-Host "代理状态:关闭"
}
}
default {
Write-Host "用法: powershell -ExecutionPolicy Bypass -File proxy_switch.ps1 {on|off|status}"
}
}
注意:PowerShell 默认禁止执行脚本,需使用 Set-ExecutionPolicy RemoteSigned 允许执行,或运行 powershell -ExecutionPolicy Bypass -File script.ps1 on。
针对特定应用的代理配置
如果需要为 curl、wget、git 等工具单独配置,脚本可以更精细:
# 仅影响 git 的代理 git config --global http.proxy http://127.0.0.1:7890 git config --global https.proxy http://127.0.0.1:7890 # 取消 git 代理 git config --global --unset http.proxy git config --global --unset https.proxy
自动检测并配置代理(高级脚本)
如果你希望脚本智能判断当前网络环境并自动配置,可以结合 curl 测试代理连通性:
#!/bin/bash
# 智能代理配置脚本
PROXY_URL="http://127.0.0.1:7890"
TEST_URL="https://www.google.com"
# 测试代理是否可用
if curl -s --proxy "$PROXY_URL" --connect-timeout 5 "$TEST_URL" > /dev/null 2>&1; then
export http_proxy="$PROXY_URL"
export https_proxy="$PROXY_URL"
echo "代理可用,已自动配置"
else
unset http_proxy https_proxy
echo "代理不可用,已关闭代理"
fi
跨平台脚本(Bash/Python 均可)
使用 Python 编写脚本,可以更优雅地实现跨平台支持:
#!/usr/bin/env python3
import os, sys, platform
def set_proxy(enabled=True, host="127.0.0.1", port=7890):
if platform.system() == "Windows":
# 修改 Windows 注册表
import winreg
key = winreg.OpenKey(winreg.HKEY_CURRENT_USER,
r"Software\Microsoft\Windows\CurrentVersion\Internet Settings",
0, winreg.KEY_SET_VALUE)
winreg.SetValueEx(key, "ProxyEnable", 0, winreg.REG_DWORD, 1 if enabled else 0)
if enabled:
winreg.SetValueEx(key, "ProxyServer", 0, winreg.REG_SZ, f"{host}:{port}")
winreg.CloseKey(key)
print(f"Windows 代理{'已开启' if enabled else '已关闭'}")
else:
# Linux/macOS 环境变量
if enabled:
os.environ['http_proxy'] = f"http://{host}:{port}"
os.environ['https_proxy'] = f"http://{host}:{port}"
else:
os.environ.pop('http_proxy', None)
os.environ.pop('https_proxy', None)
print(f"环境变量代理{'已设置' if enabled else '已移除'}")
if __name__ == "__main__":
if len(sys.argv) > 1 and sys.argv[1] == "off":
set_proxy(False)
else:
set_proxy(True)
注意事项
- 权限问题:修改系统代理(Windows 注册表)可能需要管理员权限。
- 环境变量作用域:在 Linux/macOS 中,
export仅影响当前 shell 和其子进程,关闭终端后失效。 - 协议支持:如果代理支持 SOCKS5,可以设置
export all_proxy="socks5://127.0.0.1:1080"。 - 应用兼容性:并非所有程序都会读取
http_proxy环境变量(如某些 GUI 应用),此时需在程序内单独配置。
这些脚本可以满足日常 快速切换代理、自动配置 的需求,如果需要更复杂的功能(如基于网络的自动切换),可以结合 cron(Linux) 或 任务计划程序(Windows) 定期执行测试脚本。