gpt4 book ai didi

windows - 批处理脚本循环变量

转载 作者:可可西里 更新时间:2023-11-01 14:36:42 25 4
gpt4 key购买 nike

这很简单,我敢打赌,我想念它,而且由于嵌套循环,我感觉它没有按我需要的方式工作。这是一个简单的脚本,我从某个地方提取并进行了一些修改以允许进行更多测试。本质上,它只是一遍又一遍地对延迟的主机执行 ping 操作,直到您将其关闭,但记录失败的响应。一切正常,但对于我的生活,我无法弄清楚如何让它更新 for 循环中使用的名称。

@echo off
setlocal enabledelayedexpansion

:: ** Enter name to identify host in log **
set name1=Test 1
set name2=Test 2
set name3=Test 3

:: ** Enter IP or HOSTNAME to test against **
set hostIP1=1.1.1.1
set hostIP2=2.2.2.2
set hostIP3=3.3.3.3

:: ** Enter Delay in milliseconds between each test **
set delayMS=2000


title -DO NOT CLOSE- Ping Test
echo DO NOT CLOSE
echo Ping Test


:loop
set namecount=1
for %%x in (%hostIP1% %hostIP2% %hostIP3%) do (
set pingline=1
set pingname=name!namecount!
for /f "delims=" %%A in ('ping -n 1 -w 2000 -l 255 %%x') do (
if !pingline! equ 2 (
set logline=!date! !time! "%%A" - %%x !%pingname%!
echo !logline! | find "TTL=">nul || echo !logline! >> pinglog.txt
)
set /a pingline+=1
)
set /a namecount+=1
)
sleep -m %delayMS%
goto loop

通常我会使用另一种语言,但我需要能在各种 Windows 服务器上运行的东西。这似乎是最可靠的方法。谢谢

最佳答案

这是不正确的:

!%pingname%!

试试这个:

!Pingname!

我不能尝试你的代码,但我做了其他小的更正:

(更新)

 @echo off
setlocal enabledelayedexpansion

:: ** Enter name to identify host in log **
set "name1=Test 1"
set "name2=Test 2"
set "name3=Test 3"

:: ** Enter IP or HOSTNAME to test against **
set "hostIP1=1.1.1.1"
set "hostIP2=2.2.2.2"
set "hostIP3=3.3.3.3"

:: ** Enter Delay in milliseconds between each test **
set /A "delayMS=2000"


title -DO NOT CLOSE- Ping Test
Echo DO NOT CLOSE
Echo Ping Test

:loop
set /A "namecount=1"
for %%x in (%hostIP1% %hostIP2% %hostIP3%) do (
set /A "pingline=1"
call set "pingname=%%name!namecount!%%"
for /f "delims=" %%A in ('ping -n 1 -w 2000 -l 255 %%x') do (
if !pingline! equ 2 (
set "logline=%date% !time! %%A - %%x !pingname!"
(echo !logline! | find "TTL=">nul) || (echo !logline! >> "pinglog.txt")
)
set /a "pingline+=1"
)
set /a "namecount+=1"
)
sleep -m %delayMS%
goto :loop

关于windows - 批处理脚本循环变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13517669/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com