CMD下Ping脚本list命令无法显示部分ID服务器问题求助
问题:CMD批处理脚本"list"命令无法显示部分ID的服务器列表
编写了一段CMD批处理脚本,可根据ID执行Ping操作。输入'list'本应显示所有已配置的服务器列表,但ID为02、03、04、09的服务器无法显示,其余服务器均正常显示。脚本如下:
@echo off setlocal enabledelayedexpansion rem Define an array to store the IP addresses set "ipArray[02]=10.0.2.120" set "ipArray[03]=10.0.3.120" set "ipArray[04]=10.0.4.120" set "ipArray[09]=10.0.9.120" set "ipArray[11]=10.0.11.120" set "ipArray[14]=10.0.14.120" set "ipArray[16]=10.0.16.120" set "ipArray[18]=10.0.18.120" set "ipArray[19]=10.0.19.120" set "ipArray[20]=10.0.20.120" set "ipArray[21]=10.0.21.120" set "ipArray[22]=10.0.22.120" set "ipArray[24]=10.0.24.120" set "ipArray[25]=10.0.25.120" set "ipArray[26]=10.0.26.120" set "ipArray[28]=10.0.28.120" set "ipArray[29]=10.0.29.120" set "ipArray[34]=10.0.34.120" set "ipArray[38]=10.0.38.120" set "ipArray[39]=10.0.39.120" set "ipArray[43]=10.0.43.120" set "ipArray[47]=10.0.47.120" set "ipArray[49]=10.0.49.120" set "ipArray[50]=10.0.50.120" set "ipArray[51]=10.0.51.120" set "ipArray[56]=10.0.56.120" rem Define an array to store the location names set "locationArray[02]=Bnei Brak" set "locationArray[03]=Bilo" set "locationArray[04]=Ashdod" set "locationArray[09]=Talpiot Jerusalem" set "locationArray[11]=Rishon LeZion Merkaz" set "locationArray[14]=Netanya" set "locationArray[16]=Eilat" set "locationArray[18]=Segula" set "locationArray[19]=Haifa" set "locationArray[20]=Tel Aviv" set "locationArray[21]=Modi'in" set "locationArray[22]=Afula" set "locationArray[24]=Rishon LeZion Mizrah" set "locationArray[25]=Rishon LeZion-Designs" set "locationArray[26]=Natzrat" set "locationArray[28]=Givat Shaul" set "locationArray[29]=Karmiel" set "locationArray[34]=Zichron Yaakov" set "locationArray[38]=Or Yehuda" set "locationArray[39]=Netivot" set "locationArray[43]=Beit Shemesh" set "locationArray[47]=Rishon LeZion Mizrah" set "locationArray[49]=Holon" set "locationArray[50]=Yirka" set "locationArray[51]=Kfar Qassem - lev Haaretz" set "locationArray[56]=Yehud" rem Count the elements in the ipArray for /F %%i in ('set ipArray[ ^| find /c "="') do set "numLocations=%%i" :input rem Prompt for user input set /p "userInput=Enter ID to ping (e.g., 56) or type 'list' for location list: " if /I "%userInput%"=="list" ( echo. echo Location Array: echo +-------+--------------------------+--------------------------+ echo ^| Index ^| Code ^| Location ^| echo +-------+--------------------------+--------------------------+ for /L %%i in (2,1,56) do ( set "index=0%%i" set "index=!index:~-2!" set "code=!ipArray[%%i]!" set "location=!locationArray[%%i]!" set "code=!code! " set "location=!location! " set "code=!code:~0,26!" set "location=!location:~0,26!" echo ^| !index! ^| !code! ^| !location! ^| ) echo +-------+--------------------------+--------------------------+ echo. goto input ) if /I "%userInput%"=="help" ( echo. echo Help: echo This program allows you to ping IP addresses based on their ID. echo To ping an IP address, enter the corresponding ID. echo To view the list of locations, type 'list'. echo To exit the program, press Ctrl+C. echo. goto input ) if not defined locationArray[%userInput%] ( echo Invalid ID. Please try again. goto input ) set "chosenID=%userInput%" set "chosenIP=!ipArray[%chosenID%]!" set "chosenLocation=!locationArray[%chosenID%]!" echo. echo Pinging IP address: !chosenIP! set "pingResult=0" rem Perform 5 pings for the chosen IP address echo. echo Ping Results: set "pingLog=" for /L %%j in (1,1,5) do ( ping !chosenIP! -n 1 >nul if !errorlevel! equ 0 ( echo Ping attempt %%j: Success set "pingLog=!pingLog!Ping attempt %%j: Success`r`n" ) else ( echo Ping attempt %%j: Failed set "pingLog=!pingLog!Ping attempt %%j: Failed`r`n" set "pingResult=1" ) ) if !pingResult! equ 0 ( echo. echo Ping successful. ) else ( echo. echo Ping failed. ) echo. rem Save the ping log to a file set "logFile=!chosenLocation!_%chosenID%.txt" echo !pingLog! > "%logFile%" echo Output saved to %logFile%. rem Define email configuration set "from=alerts@lighting.co.il" set "to=yuval@lighting.co.il" set "smtpServer=mail.lighting.co.il" rem Save the ping log with proper line breaks to a temporary file set "tempFile=temp_!chosenLocation!_%chosenID%.txt" echo !pingLog! > "!tempFile!" rem Send the email using the content of the temporary file powershell.exe -ExecutionPolicy Bypass -Command "Send-MailMessage -From '%from%' -To '%to%' -Subject 'Ping Log - !chosenLocation! (ID: !chosenID!)' -Body (Get-Content -Path '!tempFile!' -Raw) -SmtpServer '%smtpServer%' -Attachments '!logFile!'" rem Remove the temporary file del "!tempFile!" echo Email sent. pause
问题原因与修复方法
问题根源
脚本中遍历列表的for /L %%i in (2,1,56)循环使用的是数字型索引(2、3、4、9),但定义的数组键是带前导零的字符串(02、03、04、09)。CMD在处理数字时会自动忽略前导零,导致!ipArray[%%i]!和!locationArray[%%i]!无法匹配到对应的数组项,所以这些ID的服务器无法显示。
修复方案
方案1:直接遍历已定义的数组键(推荐)
替换原有的for /L循环为以下代码,直接通过set ipArray[获取所有已配置的数组键,无需手动指定范围,彻底避免索引不匹配问题:
for /F "tokens=2 delims=[]=" %%k in ('set ipArray[') do ( set "index=%%k" set "code=!ipArray[%%k]!" set "location=!locationArray[%%k]!" set "code=!code! " set "location=!location! " set "code=!code:~0,26!" set "location=!location:~0,26!" echo ^| !index! ^| !code! ^| !location! ^| )
方案2:修改数字循环的索引匹配逻辑
如果坚持使用数字循环,需要在循环内始终使用带前导零的索引访问数组,并过滤未配置的ID:
for /L %%i in (2,1,56) do ( set "index=0%%i" set "index=!index:~-2!" set "code=!ipArray[!index!]!" set "location=!locationArray[!index!]!" set "code=!code! " set "location=!location! " set "code=!code:~0,26!" set "location=!location:~0,26!" if defined code echo ^| !index! ^| !code! ^| !location! ^| )
内容的提问来源于stack exchange,提问作者Euclid-bytes
相关产品推荐
相关产品推荐

