从多个源IP ping一台计算机。从文本中读取源IP。将结果写入文本或网页

时间:2012-04-19 10:01:55

标签: html batch-file ping

我需要ping多个源IP中的一个IP,并将结果写入文件或HTML页面以获取结果。我需要得到所有4个ping的结果,包括TTL以及4个ping中有多少掉线和ping时间。

我知道使用IPv6,您可以在ping命令中使用-S触发器来指定源IP,但是如何在IPv4环境中执行此操作。

设定:

Windows 2003服务器R2标准版x64。 包含85个IPv4地址的.txt文件我想用作源IP 1个目标IPv4地址 将从一个中央服务器运行 将以管理员身份登录中央服务器

期望的结果: 显示按源IP分组的结果的HTML页面 结果包括所有4个ping回复时间,TTL,发送的总数据包,接收的总数据包和以ms为单位的平均ping时间

现在让我复杂一点。我不在允许我安装任何其他软件但不包含在Windows 2003服务器R2标准x64正常安装中的环境中。自写.bat文件以来,我的内存在.bat文件上仍然很弱。所有IP地址都是IPv4。虽然我有所有这些服务器的FQDN,但我需要使用IP(出于某种原因)

我有以下内容来创建所需的页面并从一个系统ping到很多,但其余的是我遇到问题的地方,因为我需要从多个ping到一个,并且我的脚本给我的唯一结果是格式正确html页面,但它只说处理=完成 我哪里出错了......请帮忙......

谢谢

---编辑---通过将ping更改为ping.exe并将%% i放在引号中 - “%% i”现在我得到的结果但不是我希望的漂亮格式。它以127.0.0.1的格式提供结果,日期为32bytes:来自....的回复,好像只是输出到文本文件一样。我想要它做的是以表格格式显示它。已经对下面的代码进行了更改。

@echo off
echo ^<HTML^>^<HEAD^><TITLE^>SERVER PING INFO^</TITLE^>^</HEAD^> >>ping-results.html
echo ^<BODY GBCOLOR=#FFFFFF” TEXT=”#000000” LINK+#0000FF” VLINK=#800080”^> >>ping-results.html
echo ^<p align="center"^>^<table border="1" width="600"^> >>ping-results.html
echo ^<tr^>^<td^>^<B^>ping results^</td^> >> ping-results.html
for /F %%i in (c:\ping-results\serverIP.txt) do (
    echo Processing %%i...
    ping.exe "%%i" > "c:\ping-results\%%i.html" /format:htable.xsl
    echo ^<tr^>^<td^>%%i^</td^> >> ping-results.html
    echo ^<td^>^<a href="c:\ping-results\%%i.html"^>Results^</a^>^</td^> >> ping-results.html
    echo ^</tr^> >> ping-results.html
)
    echo ^<p align="center"^>^<b^>^<font size="+1" color="Red"^>^<BR^>Completed at >> ping-results.html time /T >> ping-results.html
echo - on >> ping-results.html
date /T >> ping-results.html
echo ^</font^>^</b^>^<BR^>^</p^> >> ping-results.html
echo.
echo DONE!!

1 个答案:

答案 0 :(得分:2)

最好在构建html文件之前运行ping命令吗?

像这样(显然将127.0.0.1更改为您的服务器#)

@echo off
setlocal enabledelayedexpansion enableextensions
set count=0
for /f "delims=" %%a in ('ping -n 4 127.0.0.1') do (
    set /a count=!count!+1
    set pingrspn!count!=%%a
)
echo Response1: !pingrspn4!
echo Response2: !pingrspn5!
echo Response3: !pingrspn6!
echo Response4: !pingrspn7!

现在您可以在“构建html文件”脚本中使用vars:pingrspn4-pingrspn7。

不错的想法虽然在运行中构建一个html,我喜欢它。

ping行上的/ format选项有一个问题,我注意到ping没有/ format参数,来自重定向符号的/ format选项是什么?

[编辑] 所以我调整了你的文件(你有一些缺少的HTML代码)来格式化结果(server.html)页面

    :: setlocal options
        setlocal enabledelayedexpansion enableextensions
        set count=0
        for /f "delims=" %%a in ('ping -n 4 127.0.0.1') do (
            set /a count=!count!+1
            set pingrspn!count!=%%a
        )
        echo Response1: !pingrspn4!
        echo Response2: !pingrspn5!
        echo Response3: !pingrspn6!
        echo Response4: !pingrspn7!
:: write results file       
echo ^<HTML^>^<HEAD^><TITLE^>SERVER PING INFO^</TITLE^>^</HEAD^> >>ping-results.html 
echo ^<BODY GBCOLOR=#FFFFFF” TEXT=”#000000” LINK+#0000FF” VLINK=#800080”^> >>ping-results.html 
echo ^<p align="center"^>^<table border="1" width="600"^> >>ping-results.html 
echo ^<tr^>^<td^>^<B^>ping results^</td^> >> ping-results.html 
for /F %%i in (serverIP.txt) do (    
echo Processing %%i...     
 :: already ran the ping command, so rem it out
rem ping.exe "%%i" > "%%i.html" /format:htable.xsl   
:: write %%i.html
echo ^<HTML^>^<HEAD^><TITLE^>Individual PING^</TITLE^>^</HEAD^> >%%i.html 
echo ^<BODY GBCOLOR=#FFFFFF” TEXT=”#000000” LINK+#0000FF” VLINK=#800080”^> >>%%i.html 
echo ^<p align="center"^>^<table border="1" width="600"^> >>%%i.html 
echo ^<tr^>^<td^>^<B^>results^</td^> >> %%i.html  
        echo ^<tr^>^<td^>!pingrspn4! ^<^/td^>^<^/tr^> >> %%i.html 
        echo ^<tr^>^<td^>!pingrspn5! ^<^/td^>^<^/tr^>  >> %%i.html
        echo ^<tr^>^<td^>!pingrspn6! ^<^/td^>^<^/tr^>  >> %%i.html
        echo ^<tr^>^<td^>!pingrspn7! ^<^/td^>^<^/tr^>  >> %%i.html
        echo ^<^/body^> ^<^/table^> ^<^/html^> >> %%i.html
 echo ^<tr^>^<td^>%%i^</td^> >> ping-results.html     
 echo ^<td^>^<a href="%%i.html"^>Results^</a^>^</td^> >> ping-results.html     
 echo ^</tr^> >> ping-results.html )     
 echo ^<p align="center"^>^<b^>^<font size="+1" color="Red"^>^<BR^>Completed at >> ping-results.html 
 time /T >> ping-results.html 
 echo - on >> ping-results.html 
 date /T >> ping-results.html 
 echo ^</font^>^</b^>^<BR^>^</p^> >> ping-results.html 
 echo ^<^/body^> ^<^/table^> ^<^/html^> >>  ping-results.html 
 echo DONE!!
 ping-results.html