将标题添加到自动ping脚本

时间:2017-01-10 20:38:38

标签: batch-file

我有一个小脚本来ping几个名为computers.txt的文本文件中的IP地址:
computers

目前的输出如下:
Ping test results

我想添加IP地址的描述如下:
Desired results

最好的方法是什么?

@echo off
set fnm=C:\Users\jelliott\Desktop\computers.txt
set lnm=C:\Users\jelliott\Desktop\results.txt

if exist %fnm% goto Label1

echo.
echo Cannot find %fnm%
echo.
Pause
goto :eof

:Label1
echo PingTest STARTED on %date% at %time% > %lnm%
echo ================================================= >> %lnm%
echo.
for /f %%i in (%fnm%) do call :Sub %%i
echo.
echo ================================================= >> %lnm%
echo PingTest ENDED on %date% at %time% >> %lnm%
echo ... now exiting
goto :eof

:Sub
echo Testing %1
set state=alive
ping -n 1 %1 | find "TTL="
if errorlevel 1 set state=dead
echo %1 is %state% >> %lnm%

1 个答案:

答案 0 :(得分:0)

将标题添加到您的computers.txt时,如下所示:

for /f "tokens=1*" %%i in (%fnm%) do call :Sub %%i "%%j"

您可以稍微更改一下脚本:

for循环变为:%%i
标记指定您想要的行的哪个部分。默认分隔符是空格和新行。因此,一行的第一个标记将保存到*,而行的其余部分(%%j)将保存到%%j,因为j在字母表中的i之后。 echo Testing %1 %~2 REM Other stuff to ping... echo %~2: %1 is %state% >> %lnm% 需要用双引号括起来,将多个单词放在一起作为一个参数。

现在更改子功能以使用第二个参数:

~

PingTest STARTED on 11.01.2017 at 7:48:52,69 ================================================= Clinic: 1.2.3.4 is alive Registration: 12.13.14.15 is dead Somwhere else with spaces: 78.79.251.123 is alive ================================================= PingTest ENDED on 11.01.2017 at 7:49:04,70 再次移除周围的引号。

示例输出:

Authentication-Results: mx.aol.com;
    spf=none (aol.com: the domain www.dealbroker.nl appears to have no SPF Record.) smtp.mailfrom=www.dealbroker.nl;