Ping 多台 PC 并添加文本 [英] 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.
我有这个代码可以 ping 不同的 PC,然后在它们在线/离线时返回.我想知道您是否可以在 bat 文件运行其 ping 测试后添加另一列,以便它旁边有计算机名称.
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
bat 文件创建了一个文件,显示如下;
The bat file creates a document that shows the following;
PingTest 于 28/07/2016 13:10:28 执行
PingTest executed on 28/07/2016 at 13:10:28
99.1.82.28 还活着
99.1.82.28 is alive
99.1.82.100 还活着
99.1.82.100 is alive
等等.
如果可能,我希望运行 bat 文件,以便显示此内容;
If possible I would like the bat file to run so it displays this;
bat 文件创建了一个文件,显示如下;
The bat file creates a document that shows the following;
PingTest 于 28/07/2016 13:10:28 执行
PingTest executed on 28/07/2016 at 13:10:28
计算机 1 : 99.1.82.28 还活着
Computer 1 : 99.1.82.28 is alive
计算机 2 : 99.1.82.100 还活着
Computer 2 : 99.1.82.100 is alive
等等.
--
将不胜感激任何帮助 &对此的指导.
Would appreciate any help & guidance on this.
谢谢.
推荐答案
你可以试试这个方案:
@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 的颜色函数 (-_°)
Another version with multi-colors : Special thanks goes to ICARUS for the color function (-_°)
@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
23/08/2016 更新
这篇关于Ping 多台 PC 并添加文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!