AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / computer / 问题 / 1833886
Accepted
Rootz
Rootz
Asked: 2024-03-06 01:25:56 +0800 CST2024-03-06 01:25:56 +0800 CST 2024-03-06 01:25:56 +0800 CST

这个批处理代码有什么问题吗?

  • 772

有人可以帮助如何找出这段代码有什么问题吗?基本上,它只是我用来修复某些问题的脚本的一部分,我可以获得“DCHP Status”的结果,给出空白结果或无效答案,例如 dhcp lan 显示 NO。我无法在某些机器上使用 powershell,因此需要使用批处理。

基本上,代码使用 psexec 连接 LAN 上的远程计算机,并运行基于 DHCP 或静态 IP 的脚本并修复某些内容,以便我使用 sccm 远程正确连接。

@echo off
setlocal enabledelayedexpansion

:: Disabling UAC
echo Disabling UAC
reg add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System" /v "EnableLUA" /t REG_DWORD /d 0 /f >nul
reg add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System" /v "ConsentPromptBehaviorAdmin" /t REG_DWORD /d 0 /f >nul

:: Listing all network adapters
echo List all network adapters
netsh interface show interface
echo.

:: Getting the name of the network adapters
echo Enter the interface name you want to fix:
set /p adapterName=

:: Validating the user input
if not defined adapterName (
    echo You must enter a network adapter name!
    exit /b
)

:: Running ipconfig and storing the output in a temporary file
echo Run ipconfig and store the output in a temporary file
ipconfig > ipconfig.txt
REM ipconfig /all > dhcp.txt
netsh interface ip show addresses name="%adapterName%" > dhcp.txt

:: Parsing the IP address, subnet mask, and default gateway from the output
for /f "tokens=2 delims=:" %%A in ('findstr /i /c:"IPv4 Address" ipconfig.txt') do (
    set "ip=%%A"
    set "ip=!ip: =!"
)

for /f "tokens=2 delims=:" %%B in ('findstr /i /c:"Subnet Mask" ipconfig.txt') do (
    set "mask=%%B"
    set "mask=!mask: =!"
)

for /f "tokens=2 delims=:" %%C in ('findstr /i /c:"Default Gateway" ipconfig.txt') do (
    set "gateway=%%C"
    set "gateway=!gateway: =!"
)

for /f "tokens=2 delims=:" %%D in ('findstr /i /b /c:"DHCP Enabled" dhcp.txt') do (
    set "DHCP=%%D"
    set "DHCP=!DHCP: =!"
    if /i "!DHCP!"=="Yes" (
        set "DHCPStatus=Yes"
    ) else (
        set "DHCPStatus=No"
    )
)

if /i "%DHCPStatus%"=="Yes" (
    goto :dhcp
) else (
    goto :static
)

:dhcp
echo.
echo Actual network configuration of host
echo IP Address: !ip!
echo Subnet Mask: !mask!
echo Default Gateway: !gateway!
echo DHCP: !DHCPStatus!
echo.

:static
echo.
echo Actual network configuration of host
echo IP Address: !ip!
echo Subnet Mask: !mask!
echo Default Gateway: !gateway!
echo DHCP: !DHCPStatus!
echo.

错误

整个代码:

@echo off && setlocal enabledelayedexpansion

set "_xptoKey=HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters"
set "_ipv6Key=HKLM\SYSTEM\CurrentControlSet\Services\Tcpip6\Parameters"
set "_UACKey=HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System"

echo/Disabling UAC & for %%i in =;( EnableLUA,ConsentPromptBehaviorAdmin 
    );= do >nul reg add "!_UACKey!" /v "%%~i" /t REG_DWORD /d 0 /f

:: Listing all network adapters interfaces
echo/List all network adapters & netsh interface show interface

:: Getting the name of the network adapters and validating the user input
echo/ & set /p "_adapterName=Enter the interface name you want to fix: " || =;(
     echo You must enter a network adapter name! & exit /b );=

:: Parsing the IP address, subnet mask, and default gateway from the output
for /f "tokens=1-2 delims=:(" %%A in =;('
     ipconfig /all ^| findstr /ic:"IPv4"
    ');= do set "_ip=%%~B" && call set "_ip=%%_ip: =%%"

for /f "tokens=1-2 delims=:(" %%A in =;('
     ipconfig /all ^| findstr /ic:"Sub"
    ');= do set "_Sub=%%~B" && call set "_Sub=%%_Sub: =%%"
    
for /f "tokens=1-2 delims=:(" %%A in =;('
     ipconfig /all ^| findstr /ic:"Gate"
    ');= do set "_Gate=%%~B" && call set "_Gate=%%_Gate: =%%"

:: Showing the current settings obtained
echo/Actual Network Configuration of Host
echo/     IP Address: !_ip!
echo/    Subnet Mask: !_sub!
echo/Default Gateway: !_gate!
echo/           DHCP: !_dhcp! & echo/ 

:: Applying configurations and executing common commands
echo/Disabling & =;( 
     echo/Disabling network adapter... Please Wait... >con
     netsh interface set interface "Wi-Fi" disable 
     timeout /nobreak 6 | echo/ Enabling network adapter... Please wait... >con
     netsh interface set interface "Wi-Fi" enable
     timeout /nobreak 6 | echo/ >con 
    );= >nul 2>&1

:: Set the IP address, subnet mask, gateway, DNS servers if the current IP is not static
netsh interface ip show addresses name="!_adapterName!" | findstr "DHCP.*Yes" >nul || =;(
     echo/Configuring the network settings...
     netsh interface ipv4 set address name="!_adapterName!" source=static address="!_ip!" mask="!_sub!" gateway="!_gate!"
     netsh interface ipv4 set dns name="!_adapterName!" static 1.1.1.1 validate=no
     netsh interface ipv4 add dns name="!_adapterName!" 1.0.0.1 index=2 validate=no
     netsh interface ipv4 delete dnsservers name="!_adapterName!" 1.2.3.4 validate=no
    );=

echo/Adding DNS sufix: [xpto.pt + xpto2.pt] 
reg add "!_xptoKey!" /f /v SearchList /t REG_SZ /d "xpto.pt,xpto2.pt" >nul

echo/Disable TCP/IP v6 for the network adapter...
reg add "!_ipv6Key!" /v "DisabledComponents" /t REG_DWORD /d 0xffffffff /f >nul

for %%i in =;( "set global randomizeidentifier=disabled","isatap set state state=disabled",
     "6to4 set state state=disabled","set privacy state=disable","set teredo disable"
    );= do netsh interface IPV6 %%~i 

echo/Testing Network Connection (DHCP^)...
ping -n 4 10.0.25.80 | findstr "ms loss" | sort /Uniq

echo/Running Flushdns and Registerdns... & >nul =;( 
     ipconfig /flushdns & ipconfig /registerdns 
    );= 2>&1 | findstr "IP Cache" | Sort /Uniq

timeout /nobreak 5 | endlocal & goto :eof
batch
  • 3 3 个回答
  • 95 Views

3 个回答

  • Voted
  1. DavidPostill
    2024-03-06T02:52:53+08:002024-03-06T02:52:53+08:00

    “DCHP Status”给出空白结果

    findstr /i /b /c:"DHCP Enabled" dhcp.txt如所写的那样被破坏了。

    • /b如果位于行的开头,则匹配模式。
    • /c:"string"使用字符串作为文字搜索字符串(可能包含空格)。

    然而:

    要匹配整个短语/句子或使用正则表达式,请使用/C 和 /R选项。

    (强调我的)

    因此,您需要删除/b 或添加/r.

    删除/b对我有用:

    F:\test>type dhcp.txt
    
    Configuration for interface "Wireless Network Connection"
        DHCP enabled:                         Yes
        IP Address:                           192.168.188.250
        Subnet Prefix:                        192.168.188.0/24 (mask 255.255.255.0)
        Default Gateway:                      192.168.188.251
        Gateway Metric:                       0
        InterfaceMetric:                      25
    
    
    F:\test>findstr /i /c:"DHCP Enabled" dhcp.txt
        DHCP enabled:                         Yes
    
    F:\test>
    

    for您还需要按如下方式更改循环:

    F:\test>type test.cmd
    @echo off
    setlocal enabledelayedexpansion
    for /f "tokens=3" %%D in ('findstr /i /c:"DHCP Enabled" dhcp.txt') do (
        if /i "%%D"=="Yes" (
            set "DHCPStatus=Yes"
        ) else (
            set "DHCPStatus=No"
        )
        echo !DHCPStatus!
      )
    endlocal
    
    F:\test>test
    Yes
    
    F:\test>
    

    进一步阅读

    • Windows CMD 命令行的 AZ 索引 | SS64.com
    • Windows CMD 命令(分类) - Windows CMD - SS64.com
    • Findstr - 搜索字符串 - Windows CMD - SS64.com
    • 1
  2. Danijel
    2024-03-06T02:44:30+08:002024-03-06T02:44:30+08:00

    你的问题是你如何使用 delims,...即,你可以看到;

    /B         Matches pattern if at the beginning of a line.
    

    但这样你就必须“尊重”你自己的输出并实际配合;

    for /f "tokens=2 delims=:" %%D in ('findstr /i /b /c:"    DHCP enabled" dhcp.txt') do (
    

    PS:这同样有效;

    for /f "tokens=2 delims=:" %%D in ('findstr /i /b /c:"    DHCP enabled" dhcp.txt') do (
        set "DHCPStatus=%%D"
    )
    
    • 0
  3. Best Answer
    Io-oI
    2024-03-07T05:51:46+08:002024-03-07T05:51:46+08:00

    您不需要创建临时文件,并且必须在操作它们以获得所需结果后删除它们,您可以使用双循环for直接从命令输出获取字符串,并且还观察标签:static基本上与以下内容相同标签,因此您可以在没有标签的情况下:DHCP使用...echo variables:specific:each


    @echo off && setlocal enabledelayedexpansion
    
    set "_xptoKey=HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters"
    set "_UACKey=HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System"
    
    echo/Disabling UAC & for %%i in =;( EnableLUA,ConsentPromptBehaviorAdmin 
        );= do >nul reg add "!_UACKey!" /v "%%~i" /t REG_DWORD /d 0 /f
        
    echo/List all network adapters & netsh interface show interface & echo/
    
    :: Getting the name of the network adapters and validating the user input
    echo/ & set /p "_adapterName=Enter the interface name you want to fix: " || =;(
         echo You must enter a network adapter name! & exit /b );=
    
    :: Parsing the IP address, subnet mask, and default gateway from the output    
    for %%G in =;( IPv4,Sub,Gate );= do for /f "tokens=1-2 delims=:(" %%A in =;('
         ipconfig /all ^| Find "%%~G" ');= do set "_%%~G=%%~B" && call set "_%%~G=%%_%%~G: =%%"
    
    :: Showing the current settings obtained
    echo/ & echo/Actual network configuration of host
    echo/     IP Address: !_IPv4! &  echo/    Subnet Mask: !_Sub!
    echo/Default Gateway: !_Gate! &  echo/           DHCP: !_DHCP! & echo/ 
    
    :: Applying configurations and executing common commands
    echo/Disabling & =;( 
         echo/Disabling network adapter... Please Wait... >con
         netsh interface set interface "Wi-Fi" disable 
         timeout /nobreak 6 | echo/ Enabling network adapter... Please wait... >con
         netsh interface set interface "Wi-Fi" enable
         timeout /nobreak 6 | echo/ >con 
        );= >nul 2>&1
    
    :: Set the IP address, subnet mask, gateway, DNS servers if the current IP is not static
    netsh interface ip show addresses name="!_adapterName!" | findstr "DHCP.*Yes" >nul || =;(
         echo/Configuring the network settings...
         netsh interface ipv4 set address name="!_adapterName!" source=static address="!_ip!" mask="!_sub!" gateway="!_gate!"
         netsh interface ipv4 set dns name="!_adapterName!" static 1.1.1.1 validate=no
         netsh interface ipv4 add dns name="!_adapterName!" 1.0.0.1 index=2 validate=no
         netsh interface ipv4 delete dnsservers name="!_adapterName!" 1.2.3.4 validate=no
        );=
    
    >nul =;(
         echo/Adding DNS sufix: [xpto.pt + xpto2.pt] >con:
         echo/reg add "!_xptoKey!" /f /v SearchList /t REG_SZ /d "xpto.pt,xpto2.pt" 
        
         echo/Disable TCP/IP v6 for the network adapter... >con:
         echo/reg add "!_xptoKey:p\=p6\!" /v "DisabledComponents" /t REG_DWORD /d 0xffffffff /f 
        );= 2>&1
    
    for %%i in =;( "set global randomizeidentifier=disabled","isatap set state state=disabled",
         "6to4 set state state=disabled","set privacy state=disable","set teredo disable"
        );= do netsh interface IPV6 %%~i 
    
    echo/Testing Network Connection (DHCP^)...
    ping -n 4 10.0.25.80 | findstr "ms loss" | sort /Uniq
    
    echo/Running /Flushdns and /RegisterDNS... & >nul =;( 
         ipconfig /flushdns & ipconfig /registerdns 
        );= 2>&1 | findstr "IP Cache" | Sort /Uniq
    
    timeout /nobreak 5 | endlocal & goto :eof
    

    • 或者最接近原始代码的替代方案......

    
    @echo off && setlocal enabledelayedexpansion
    
    echo/Disabling UAC & for %%i in =;( EnableLUA,ConsentPromptBehaviorAdmin 
        );= do >nul reg add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System" /v "%%~i" /t REG_DWORD /d 0 /f
    
    :: Listing all network adapters
    echo/List all network adapters & netsh interface show interface
    
    :: Getting the name of the network adapters and validating the user input
    echo/ & set /p "_adapterName=Enter the interface name you want to fix: " || =;(
         echo You must enter a network adapter name! & exit /b );=
    
    :: Parsing the IP address, subnet mask, and default gateway from the output
    for /f "tokens=1-2 delims=:(" %%A in =;('
         ipconfig /all ^| findstr /ic:"IPv4"
        ');= do set "_ip=%%~B" && call set "_ip=%%_ip: =%%"
    
    for /f "tokens=1-2 delims=:(" %%A in =;('
         ipconfig /all ^| findstr /ic:"Sub"
        ');= do set "_Sub=%%~B" && call set "_Sub=%%_Sub: =%%"
        
    for /f "tokens=1-2 delims=:(" %%A in =;('
         ipconfig /all ^| findstr /ic:"Gate"
        ');= do set "_Gate=%%~B" && call set "_Gate=%%_Gate: =%%"
    
    netsh interface ip show addresses name="%_adapterName%" | findstr "DHCP.*Yes" >nul && =;(
        set "_dhcp=Yes" && goto :dhcp );= || set "_dhcp=No" && goto :static
    
    :dhcp
    echo/
    echo/Actual network configuration of host
    echo/     IP Address: !_ip!
    echo/    Subnet Mask: !_sub!
    echo/Default Gateway: !_gate!
    echo/           DHCP: !_dhcp! & echo/ 
    endlocal & goto :eof
    
    :static
    echo/
    echo/Actual network configuration of host
    echo/     IP Address: !_ip!
    echo/    Subnet Mask: !_sub!
    echo/Default Gateway: !_gate!
    echo/           DHCP: !_dhcp!
    echo/           DHCP: !_dhcp! & echo/ 
    endlocal & goto :eof
    

    请注意,您当前正在执行重定向输出的命令,并将此输出提交到文件以在循环中进行操作,并检查行中是否有“是”/“否”来定义此变量并将执行带到流程控制(或goto :DHCP标签goto :static) ,但只需一行即可完成此操作,而不需要执行上述所有步骤。

    请参阅使用findstrfor 条件,其中要在命令的行/输出 ( ) 中搜索多个字符串findstr String1.*String2,并将其重定向到运算符,该运算符将直接定义变量,具体取决于结果是 true/success ( &&) 还是 false /失败 (||):

    如果字符串DHCP+一个或多个字符( .*)+Yes在同一行找到返回true,则后面执行&& (executes these commands),但如果执行返回false,多个字符串在同一行没有找到,则操作符|| (execute this block):

    netsh interface ip show addresses name="%_adapterName%" | findstr "DHCP.*Yes" >nul && =;(
        set "_dhcp=Yes" && goto :dhcp );= || set "_dhcp=No" && goto :static

    Endlocal最后,请始终考虑在使用时添加Setlocal,尤其是在不关闭当前会话的情况下多次执行同一 bat 的情况下。


    附言。:考虑DavidPostill 的回答/B,他的未定义变量的核心是在您的...命令中使用findstr。


    其他资源:

    • Goto :lable /?
    • Findstr /?
    • For /?
    • For /F /?
      • For /F commands
    • 条件执行
      • ||和&&
    • 命令重定向
      • |,<,>,2>, ETC。
    • 又GOTO :EOF回到哪里呢?
    • Set variable=variable:substrings| DOS——字符串操作
    • Windows命令解释器[ ]如何cmd.exe解析脚本
    • -1

相关问题

  • 从命令行计算具有特定扩展名 (.mp4) 的文件夹和子文件夹中的文件

  • Windows批处理文件最小化所有打开的窗口

  • Windows 批处理更改所有 IIS 应用程序池超时

  • 命令行:文件重命名的通用命令

  • Python 的“pass”参数的批处理等价物是什么?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何减少“vmmem”进程的消耗?

    • 11 个回答
  • Marko Smith

    从 Microsoft Stream 下载视频

    • 4 个回答
  • Marko Smith

    Google Chrome DevTools 无法解析 SourceMap:chrome-extension

    • 6 个回答
  • Marko Smith

    Windows 照片查看器因为内存不足而无法运行?

    • 5 个回答
  • Marko Smith

    支持结束后如何激活 WindowsXP?

    • 6 个回答
  • Marko Smith

    远程桌面间歇性冻结

    • 7 个回答
  • Marko Smith

    子网掩码 /32 是什么意思?

    • 6 个回答
  • Marko Smith

    鼠标指针在 Windows 中按下的箭头键上移动?

    • 1 个回答
  • Marko Smith

    VirtualBox 无法以 VERR_NEM_VM_CREATE_FAILED 启动

    • 8 个回答
  • Marko Smith

    应用程序不会出现在 MacBook 的摄像头和麦克风隐私设置中

    • 5 个回答
  • Martin Hope
    Vickel Firefox 不再允许粘贴到 WhatsApp 网页中? 2023-08-18 05:04:35 +0800 CST
  • Martin Hope
    Saaru Lindestøkke 为什么使用 Python 的 tar 库时 tar.xz 文件比 macOS tar 小 15 倍? 2021-03-14 09:37:48 +0800 CST
  • Martin Hope
    CiaranWelsh 如何减少“vmmem”进程的消耗? 2020-06-10 02:06:58 +0800 CST
  • Martin Hope
    Jim Windows 10 搜索未加载,显示空白窗口 2020-02-06 03:28:26 +0800 CST
  • Martin Hope
    andre_ss6 远程桌面间歇性冻结 2019-09-11 12:56:40 +0800 CST
  • Martin Hope
    Riley Carney 为什么在 URL 后面加一个点会删除登录信息? 2019-08-06 10:59:24 +0800 CST
  • Martin Hope
    zdimension 鼠标指针在 Windows 中按下的箭头键上移动? 2019-08-04 06:39:57 +0800 CST
  • Martin Hope
    jonsca 我所有的 Firefox 附加组件突然被禁用了,我该如何重新启用它们? 2019-05-04 17:58:52 +0800 CST
  • Martin Hope
    MCK 是否可以使用文本创建二维码? 2019-04-02 06:32:14 +0800 CST
  • Martin Hope
    SoniEx2 更改 git init 默认分支名称 2019-04-01 06:16:56 +0800 CST

热门标签

windows-10 linux windows microsoft-excel networking ubuntu worksheet-function bash command-line hard-drive

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve