【问题标题】:Pinging Multiple PCs and Adding TextPing 多台 PC 并添加文本
【发布时间】:2016-07-28 12:19:08
【问题描述】:

我对此很陌生,所以请多多包涵,如果您需要我提供更多信息,请说。在此先感谢您的帮助。

我有这段代码可以 ping 不同的 PC,然后在它们在线/离线时返回。我想知道你是否可以在 bat 文件运行 ping 测试后添加另一列,以便它旁边有计算机名称。

 @echo off
 if exist C:\tools\computers.txt goto Label1 
 echo.
 echo Cannot find C:\tools\computers.txt
 echo.
 Pause 
 goto :eof

:Label1 
 echo PingTest executed on %date% at %time% > C:\tools\z.txt
 echo ================================================= >> C:\tools\z.txt
 for /f %%i in (C:\tools\computers.txt) do call :Sub %%i notepad C:\tools\z.txt 
 goto :eof

:Sub
echo Testing %1 set state=alive ping -n 1 %1 | find /i "bytes=" || set state=dead echo %1 is %state% >> C:\tools\z.txt

bat 文件创建一个显示以下内容的文档;

PingTest 于 2016 年 7 月 28 日 13:10:28 执行

99.1.82.28 还活着

99.1.82.100 还活着

等。


如果可能的话,我希望 bat 文件运行,以便显示它;

bat 文件创建一个显示以下内容的文档;

PingTest 于 2016 年 7 月 28 日 13:10:28 执行

计算机 1:99.1.82.28 还活着

计算机 2:99.1.82.100 还活着

等。

--

在这方面的任何帮助和指导将不胜感激。

谢谢。

【问题讨论】:

标签: batch-file text ping


【解决方案1】:

你可以试试这个解决方案:

@echo off
Title Ping Test
set "URLS=URLS.txt"
set "LogFile=PingResults.txt"
If exist %LogFile% Del %LogFile%
(
    echo ******************************************************
    echo   PingTest executed on %Date% @ Time %Time% 
    echo ******************************************************
    echo(
) > %LogFile%

Setlocal EnableDelayedExpansion
for /f "usebackq delims=" %%a in ("%URLS%") do (
    for /f "tokens=2 delims=[]" %%b in ('ping -n 1 %%a') do set "ip=%%b"
        ping -n 1 %%a>nul && set "msg=%%a : !ip! ALive ok" || set "msg=%%a : !ip! Dead failed to respond"
        echo !msg!
        echo !msg! >> %LogFile%
    ) 
)
EndLocal
Start "" %LogFile%
pause>nul & exit

编辑:29/07/2016 @ 12:48

另一个多色版本:特别感谢 ICARUS 的颜色函数 (-_°)

@echo off
Rem Special thanks goes to Iracus for the color function (-_°)
mode con cols=60 lines=20
Title Multi-Ping hosts Tester with Multi-colors by Hackoo
set "URLS=URLS.txt"
set "LogFile=PingResults.txt"
If exist %LogFile% Del %LogFile%
call :init
echo(
call :color 0E "------- Ping Status of Computers hosts -------" 1
echo(
(
    echo ******************************************************
    echo   PingTest executed on %Date% @ Time %Time% 
    echo ******************************************************
    echo(
) > %LogFile%
Setlocal EnableDelayedExpansion
for /f "usebackq delims=" %%a in ("%URLS%") do (
    for /f "tokens=2 delims=[]" %%b in ('ping -n 1 %%a') do set "ip=%%b"
        ping -n 1 %%a>nul && set "msg=%%a - !ip! ALive ok" && Call :Color 0A "!msg!" 1 || set "msg=%%a - !ip! Dead failed to respond" && Call :Color 0C "!msg!" 1
        echo !msg! >> %LogFile%
    ) 
)
EndLocal
Start "" %LogFile%
pause>nul & exit

:init
prompt $g
for /F "delims=." %%a in ('"prompt $H. & for %%b in (1) do rem"') do set "BS=%%a"
exit /b

:color
set nL=%3
if not defined nL echo requires third argument & pause > nul & goto :eof
if %3 == 0 (
    <nul set /p ".=%bs%">%2 & findstr /v /a:%1 /r "^$" %2 nul & del %2 2>&1 & goto :eof
) else if %3 == 1 (
    echo %bs%>%2 & findstr /v /a:%1 /r "^$" %2 nul & del %2 2>&1 & goto :eof
)
exit /b

编辑:2016 年 8 月 23 日更新

http://pastebin.com/zjYwSqUM

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-25
    相关资源
    最近更新 更多