如何在 Windows 7 中从命令行更改代理设置?
我说的不只是http_proxy
。我需要设置系统范围的代理设置(Internet 属性设置中的设置)。我该怎么做?
答案1
您需要配置一个注册表脚本,以便进行您通常通过控制面板进行的更改,然后合并该脚本以启用代理。您还需要一个“撤消”注册表脚本来禁用更改。
就我而言,我有两个脚本,enable.reg 和 disable.reg:
启用代理:
Windows Registry Editor Version 5.00
[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings]
"AutoConfigURL"="http://10.10.10.1/autoproxy/proxy.pac"
[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Connections]
"DefaultConnectionSettings"=hex:16,00,00,00,05,02,00,00,0d,00,00,00,0e,00,00,\
00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00
"SavedLegacySettings"=hex:36,00,00,00,46,1a,00,00,0d,00,00,00,0e,00,00,00,32,\
00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00
禁用代理:
Windows Registry Editor Version 5.00
[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings]
"AutoConfigURL"=-
[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Connections]
"DefaultConnectionSettings"=hex:16,00,00,00,05,02,00,00,0d,00,00,00,0e,00,00,\
00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00
"SavedLegacySettings"=hex:36,00,00,00,46,1a,00,00,0d,00,00,00,0e,00,00,00,32,\
00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00
在“禁用”脚本中,=-
AutoConfigURL 末尾实际上从注册表中删除了该项。
请注意,您在上面看到的值已针对此答案进行了修改。实际的十六进制值要长得多。
为了使用这些脚本,我为每个脚本都准备了一个批处理文件,如下所示:
@echo off
start /min reg import C:\Path\To\Registry\File\enable_proxy.reg
这完全可以通过命令行实现。
答案2
简单有效的解决方案取自http://www.ehow.com/how_6887864_do-proxy-settings-command-prompt_.html
启用代理使用的命令:
reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" ^
/v ProxyEnable /t REG_DWORD /d 1 /f
禁用代理使用的命令:
reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" ^
/v ProxyEnable /t REG_DWORD /d 0 /f
更改代理地址的命令:
reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" ^
/v ProxyServer /t REG_SZ /d proxyserveraddress:proxyport /f
我添加了行继续符 (^) 以提高可读性。此外,在这种情况下,它更像是针对每个用户的设置,而不是系统范围的设置。
答案3
网易来救援!
NetSh winhttp set proxy
应该会有所帮助。命令如下:
netsh winhttp set proxy myproxy
netsh winhttp set proxy myproxy:80 "<local>bar"
netsh winhttp set proxy proxy-server="http=myproxy;https=sproxy:88" bypass-list="*.contoso.com"
答案4
创建一个批处理文件并粘贴以下内容(它将切换代理状态),
@echo off
FOR /F "tokens=2* delims= " %%A IN ('REG QUERY "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v ProxyEnable') DO SET currentProxy=%%B
rem ECHO currentProxy=%currentProxy%
if %currentProxy%==0x1 (
reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v ProxyEnable /t REG_DWORD /d 0 /f
echo Proxy Disabled
) else (
reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v ProxyEnable /t REG_DWORD /d 1 /f
echo Proxy Enabled
)
pause