【发布时间】:2017-10-23 14:12:17
【问题描述】:
这里是业余脚本编码器,我正在寻求一些帮助,以使我的循环脚本以所需的方式工作。
所以我有 9 个要 ping 的 IP 地址列表,它们存储在设置为 2 个字母后跟 1 个数字的变量中,如下所示。
我认为我面临两个问题,无论如何我都可以看到。 第一个是当试图调用存储在变量中的 IP 时,它实际上试图 ping 我想要的变量的名称,而不是我想要调用的变量中的 IP。
我知道的第二个问题是如何增加变量的那个数字。 IE如何让“AP1”递增到“AP2”等等。
帮助! :(
以下是我的脚本的精简版,用于展示我的情况。在完整的脚本中有许多不同的“站点”
echo off
setlocal enabledelayedexpansion
cls
:SET_SITE_ID
echo There are 3 customers set up: [1] [2] [3]
set /p SITE="Enter Site ID: "
goto %SITE%
:VERIFY_SITE_ID
cls
echo Site ID set to: %SITE%
echo Location: %LOCATION%
echo The number of AP's is: %AP_QUANTITY%
echo The controller IP is: %CONTROLLER%
echo AP1 IP: %AP1%
if defined AP2 echo AP2 IP: %AP2%
if defined AP3 echo AP3 IP: %AP3%
if defined AP4 echo AP4 IP: %AP4%
if defined AP5 echo AP5 IP: %AP5%
if defined AP6 echo AP6 IP: %AP6%
if defined AP7 echo AP7 IP: %AP7%
if defined AP8 echo AP8 IP: %AP8%
if defined SITE_NOTES echo Site Notes: %SITE_NOTES%
choice /C 12 /N /M "Is the above info correct? [1=Proceed to Ping Test] [2=Change it]"
if %errorlevel%==1 goto PING_TEST
if %errorlevel%==2 goto SET_SITE_ID
:PING_TEST
cls
choice /C 12 /N /M "Which test script? [1=Original] [2=Stephan's script]"
if %errorlevel%==1 goto PING_TEST_ORIGINAL
if %errorlevel%==2 goto PING_TEST_STEPHAN
:PING_TEST_ORIGINAL
cls
echo Starting ping test.
echo -------------------------------------------------------------------------------
echo Testing Controller...
ping -n 2 %CONTROLLER%
echo -------------------------------------------------------------------------------
echo Testing AP1 (of %AP_QUANTITY%)...
ping -n 2 %AP1%
if not defined AP2 goto END_OF_PING_TEST
echo -------------------------------------------------------------------------------
echo Testing AP2 (of %AP_QUANTITY%)...
ping -n 2 %AP2%
if not defined AP3 goto END_OF_PING_TEST
echo -------------------------------------------------------------------------------
echo Testing AP3 (of %AP_QUANTITY%)...
ping -n 2 %AP3%
if not defined AP4 goto END_OF_PING_TEST
echo -------------------------------------------------------------------------------
echo Testing AP4 (of %AP_QUANTITY%)...
ping -n 2 %AP4%
if not defined AP5 goto END_OF_PING_TEST
echo -------------------------------------------------------------------------------
echo Testing AP5 (of %AP_QUANTITY%)...
ping -n 2 %AP5%
if not defined AP6 goto END_OF_PING_TEST
echo -------------------------------------------------------------------------------
echo Testing AP6 (of %AP_QUANTITY%)...
ping -n 2 %AP6%
if not defined AP7 goto END_OF_PING_TEST
echo -------------------------------------------------------------------------------
echo Testing AP7 (of %AP_QUANTITY%)...
ping -n 2 %AP7%
if not defined AP8 goto END_OF_PING_TEST
echo -------------------------------------------------------------------------------
echo Testing AP8 (of %AP_QUANTITY%)...
ping -n 2 %AP8%
if not defined AP9 goto END_OF_PING_TEST
echo -------------------------------------------------------------------------------
echo Testing AP9 (of %AP_QUANTITY%)...
ping -n 2 %AP9%
goto END_OF_PING_TEST
:PING_TEST_STEPHAN
set count=0
for /f "tokens=2 delims==" %%a in ('set AP') do (
set /a count+=1
echo -------------------------------------------------------------------------------
echo testing %%a, [!count! of %AP_QUANTITY%]
ping -n 2 %%a
)
goto END_OF_PING_TEST
:END_OF_PING_TEST
echo -------------------------------------------------------------------------------
choice /C YN /M "End of ping test. Repeat?"
if %errorlevel%==1 goto PING_TEST
if %errorlevel%==2 exit
:1
set LOCATION=Customer 1
set AP_QUANTITY=1
set CONTROLLER=192.168.0.253
set AP1=192.168.0.252
set SITE_NOTES=Notes go here
goto VERIFY_SITE_ID
:2
set LOCATION=Customer 2
set AP_QUANTITY=2
set CONTROLLER=192.168.0.253
set AP1=192.168.0.252
set AP2=192.168.0.111
set SITE_NOTES=Notes go here
goto VERIFY_SITE_ID
:3
set LOCATION=Customer 3
set AP_QUANTITY=9
set CONTROLLER=192.168.0.253
set AP1=192.168.0.252
set AP2=192.168.0.111
set AP3=192.168.0.8
set AP4=127.0.0.1
set AP5=127.0.0.1
set AP6=127.0.0.1
set AP7=127.0.0.1
set AP8=127.0.0.1
set AP9=127.0.0.1
set SITE_NOTES=Notes go here
goto VERIFY_SITE_ID
【问题讨论】:
-
哦,对不起。 Windows 命令行批处理文件
标签: loops batch-file cmd command increment