windows下cmd配置代理脚本

windows下配置代理脚本

注意,写的是cmd使用的bat脚本,不适用于powershell

@echo off

@Rem setlocal endlocal可以避免污染调用者环境 https://zzz.buzz/zh/notes/windows-batch/
@Rem 开启变量延迟扩展, 参考: https://www.cnblogs.com/ydhliphonedev/archive/2012/09/25/2702092.html

setlocal enabledelayedexpansion


@Rem for循环的说明,参考: https://blog.csdn.net/Ctrain/article/details/47917573
@Rem 获取本机ip的说明, 参考:https://www.cnblogs.com/xwdreamer/p/3848703.html

for /f "tokens=2 delims=:" %%b in ('ipconfig^|find /i "ip"') do (

  set ip_str_with_space=%%b
  @Rem echo original str: "!ip_str_with_space!"
  
  @Rem 去掉首尾空格,参考:http://www.bathome.net/thread-453-1-1.html
  
  for /f "tokens=*" %%i in ("!ip_str_with_space!") do set fsip=%%~nxi
  
  @Rem echo;fsip: "!fsip!"
  
  @Rem 过滤掉一些网段
  @Rem 参考,http://www.bathome.net/thread-59222-1-1.html
  
  echo;!fsip!|findstr /b "192.168.64 169.254 2.0.0">nul 2>&1 || (
    echo;usedIp: "!fsip!"
    set HTTP_PROXY1=http://!fsip!:10809
    set HTTPS_PROXY1=http://!fsip!:10809
    set NO_PROXY1=localhost,127.0.0.1,10.0.0.0/8,192.168.0.0/16,172.16.0.0/12
    
    @Rem echo;!HTTP_PROXY1!
    @Rem echo;!HTTPS_PROXY1!
    @Rem echo;!NO_PROXY1!
  ) 
  
)
@Rem setlocal endlocal可以避免污染调用者环境,
@Rem 但是有几个结果需要导出
endlocal &set HTTP_PROXY=%HTTP_PROXY1%&set HTTPS_PROXY=%HTTPS_PROXY1%&set NO_PROXY=%NO_PROXY1%

@Rem 输出一下结果,注意setlocal endlocal之外的变量,会暴露到父cmd,这与linux中shell不一样
echo;%HTTP_PROXY%
echo;%HTTPS_PROXY%
echo;%NO_PROXY%


@Rem 别人的一些经验总结:https://zzz.buzz/zh/notes/windows-batch/
@Rem 关于@, rem, @rem的含义和区别: https://blog.csdn.net/chenyunku/article/details/97641069

如果确实需要在powershell中执行,考虑:https://www.51cto.com/article/380594.html


评论

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注