Ping多台PC并添加文本 [英] Pinging Multiple PCs and Adding Text

查看:86
本文介绍了Ping多台PC并添加文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:\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文件创建一个显示以下内容的文档;

The bat file creates a document that shows the following;

PingTest于2016年7月28日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于2016年7月28日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.

谢谢.

推荐答案

您可以尝试以下解决方案:

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

于29/07/2016 @ 12:48

EDIT : on 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更新

EDIT : Update On 23/08/2016

http://pastebin.com/zjYwSqUM

这篇关于Ping多台PC并添加文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆