Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
666 views
in Technique[技术] by (71.8m points)

batch file - Pinging Multiple PCs and Adding Text

I'm pretty new to this so please bear with me, and if you require anymore information from me please say. Thanks in advance for your help.

I have this code that pings different PCs then returns back if they are online/offline. I wanted to know if you could add another column once the bat file has ran its ping test so it has the computer name next to it.

 @echo off
 if exist C:oolscomputers.txt goto Label1 
 echo.
 echo Cannot find C:oolscomputers.txt
 echo.
 Pause 
 goto :eof

:Label1 
 echo PingTest executed on %date% at %time% > C:oolsz.txt
 echo ================================================= >> C:oolsz.txt
 for /f %%i in (C:oolscomputers.txt) do call :Sub %%i notepad C:oolsz.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:oolsz.txt

The bat file creates a document that shows the following;

PingTest executed on 28/07/2016 at 13:10:28

99.1.82.28 is alive

99.1.82.100 is alive

ect.


If possible I would like the bat file to run so it displays this;

The bat file creates a document that shows the following;

PingTest executed on 28/07/2016 at 13:10:28

Computer 1 : 99.1.82.28 is alive

Computer 2 : 99.1.82.100 is alive

ect.

--

Would appreciate any help & guidance on this.

Thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can try this solution :

@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

EDIT : on 29/07/2016 @ 12:48

Another version with multi-colors : Special thanks goes to ICARUS for the color function (-_°)

enter image description here

@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

EDIT : Update On 23/08/2016

http://pastebin.com/zjYwSqUM


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...