调整批量ping [英] Grepping a batch ping

查看:255
本文介绍了调整批量ping的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

寻找一个更好的方法来做这个,而不是我常用的手边的方法,因为这是一个我必须经常相处的过程。

Looking for a better way to do this rather than the "by-hand" method I'm used to, because it's a process I have to go through fairly regularly.

我有一系列IP可以ping,从 10.0.1.15 10.0.50.15 。第三个八位字节指的是物理位置,最后一个八位字节指的是该位置的设备。我需要查看哪些位置没有连接到网络的设备。

I have a range of IPs to ping, from 10.0.1.15 to 10.0.50.15. The third octet refers to a physical location and the last octet refers to a device at that location. I need to see which locations DO NOT have that device hooked up to the network.

我当前的解决方案是:

FOR /L %i IN (1,1,50) DO ping -n 1 10.0.%i.15 >> C:\path\to\output\file.txt

Pinging 10.0.1.15 with 32 bytes of data:
Reply from 10.0.1.15: bytes=32 time=68ms TTL=255

Ping statistics for 10.0.1.15:
    Packets: Sent = 1, Received = 1, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 68ms, Maximum = 68ms, Average = 68ms

Pinging 10.0.2.15 with 32 bytes of data:
Request timed out.

Ping statistics for 10.0.2.15:
    Packets: Sent = 1, Received = 0, Lost = 1 (100% loss),
...

我更喜欢输出更像:

2
...

在这种情况下,位置1具有设备,但位置2不存在。

In this case, location 1 has the device present, but location 2 does not.

目前,我所做的最好的解决方案是手动查看该文件,并跟踪。我可以在Python中一起攻击,但它听起来更麻烦比它的价值。我知道 grep 有一些能力来显示上下文,但我在Windows上,除了基本的NT CLI工具,没有访问任何东西。

Currently the best solution I have for doing this is to look through that file by hand and keep track. I could hack something together in Python but it sounds like more trouble than it's worth. I know that grep has some ability to show context, but I'm on Windows and don't have access to anything but the basic NT CLI tools.

有没有办法利用 findstr (或等)来获得一个更容易人类可读的输出?有没有更好的方法在例如。 Powershell?

Is there some way to leverage findstr (or etc) to get a more easily human-readable output? Is there some better method in e.g. Powershell?

推荐答案

当然。您可以使用 find (如果愿意,也可以使用 findstr )来测试TTL / strike> TTL =(编辑:请参阅下面的MC ND的注释),并使用结果ERRORLEVEL确定成功或失败。

Sure. You can use find (or findstr if you prefer) to test for the existence of "TTL" "TTL=" (edit: See MC ND's comment below) and use the resulting ERRORLEVEL to determine success or fail.

@echo off
setlocal

for /L %%I in (1,1,50) do (
    ping -n 1 10.0.%%I.15 | find /i "TTL=" >NUL && (
        echo 10.0.%%I.15: Online
    ) || (
        echo 10.0.%%I.15: OFFLINE
    )
)

查看此页面有关条件执行的更多信息(&& || stuff)。 成功时将设置为设置为0,如果失败则设置为1。如果你想要的结果记录到一个txt文件而不是控制台,只需将整个东西包装在另一个括号代码块,如下:

See this page for more info on conditional execution (the && and || stuff). find sets ERRORLEVEL to 0 on success, or 1 on fail. If you want the results logged to a txt file rather than the console, just wrap the whole thing in another parenthetical code block like this:

@echo off
setlocal

set "logfile=C:\path\to\output\file.txt"

>> "%logfile%" (
    for /L %%I in (1,1,50) do (
        ping -n 1 10.0.%%I.15 | find /i "TTL=" >NUL && (
            echo 10.0.%%I.15: Online
        ) || (
            echo 10.0.%%I.15: OFFLINE
        )
    )
)

type "%logfile%"

这篇关于调整批量ping的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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