批处理文件 - 循环 ping - 输出到已启动的文件主机

发布于 2024-10-18 03:25:11 字数 227 浏览 2 评论 0原文

我想制作一个 .bat 文件,它将执行如下所示的 for 循环:

@echo off
FOR /L %%G IN (1, 1, 69) DO (
    ping -n 1 192.168.%%G.3 
    ping -n 1 192.168.%%G.4
)

然后查看输出并仅将成功回复 ping 的 IP 发送到 txt 文件。这可以通过 CMD 批处理文件实现吗?

I would like to make a .bat file that will do a for loop like below:

@echo off
FOR /L %%G IN (1, 1, 69) DO (
    ping -n 1 192.168.%%G.3 
    ping -n 1 192.168.%%G.4
)

Then look through the output and send only the IPs that replied successfully to the ping to a txt file. Is this possible with a CMD batch file?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

初见 2024-10-25 03:25:11
@ECHO OFF
SET output=%USERPROFILE%\output.txt
IF EXIST "%output%" DEL "%output%"
FOR /L %%G IN (1, 1, 69) DO (
    CALL :ping 192.168.%%G.3
    CALL :ping 192.168.%%G.4
)
GOTO :EOF

:ping
ping -n 1 %1 >NUL && ECHO %1>>"%output%"

基本上,您使用 && 添加仅在前一个命令(&& 之前的命令)成功完成时才执行的命令(从技术上讲,返回退出代码 0)。

对于相反的情况也有类似的方法。如果您想对命令的不成功结果执行某些操作,请将 || 放在它后面,然后是执行操作的命令。

编辑

关于ping的一点说明。有时您会从路由器收到主机不可访问的通知。在这种情况下,ping 仍然会退出并显示 0 代码(“成功”),因为它确实收到了回复,即使它来自路由器而不是来自实际主机。

如果您的主机就是这种情况,并且您不想在输出文件中出现此类误报,则必须解析 ping 的输出以获取一些关键字,以指示 ping 是否是确实成功了。到目前为止,您可以依赖显示汇总统计信息的行:它们仅在回复来自预期主机时才会出现。

因此,这是替代方法:

@ECHO OFF
SET output=%USERPROFILE%\output.txt
IF EXIST "%output%" DEL "%output%"
FOR /L %%G IN (1, 1, 69) DO (
    CALL :ping 192.168.%%G.3
    CALL :ping 192.168.%%G.4
)
GOTO :EOF

:ping
ping -n 1 %1 | find "Approximate round trip" >NUL && ECHO %1>>"%output%"

编辑 2

将两种解决方案更改为使用子例程调用,以避免 for 内的 %ip% 过早扩展环形。 (也可以通过启用延迟扩展来修复。)

还在各处引用 %output%

@ECHO OFF
SET output=%USERPROFILE%\output.txt
IF EXIST "%output%" DEL "%output%"
FOR /L %%G IN (1, 1, 69) DO (
    CALL :ping 192.168.%%G.3
    CALL :ping 192.168.%%G.4
)
GOTO :EOF

:ping
ping -n 1 %1 >NUL && ECHO %1>>"%output%"

Basically, you use && to add the command that is only executed if the previous command (the one before the &&) completed successfully (technically speaking, returned the exit code of 0).

There's a similar approach for the opposite case. If you want to perform some actions on the unsuccessful result of a command, you put || after it and then the command implementing your action.

EDIT

One note about ping. Sometimes you get a notification from the router that the host is not accessible. In this case ping still exits with 0 code ('successful'), because it does get a reply, even if it's from the router and not from the actual host.

If that can be the case with your hosts and you don't want to have such false positives in the output file, you'll have to parse the output of ping for some keywords indicating whether the pinging was successful indeed. So far you can rely on the lines showing the aggregate stats: they only appear if the reply was from the intended host.

So, here's the alternative approach:

@ECHO OFF
SET output=%USERPROFILE%\output.txt
IF EXIST "%output%" DEL "%output%"
FOR /L %%G IN (1, 1, 69) DO (
    CALL :ping 192.168.%%G.3
    CALL :ping 192.168.%%G.4
)
GOTO :EOF

:ping
ping -n 1 %1 | find "Approximate round trip" >NUL && ECHO %1>>"%output%"

EDIT 2

Changed both solutions to use subroutine call in order to avoid premature expansion of %ip% inside the for loop. (Could also be fixed by enabling delayed expansion instead.)

Also quoted %output% everywhere.

护你周全 2024-10-25 03:25:11
@echo off
:A
FOR /L %%G IN (1, 1, 69) DO (
    ping -n 1 192.168.%%G.3 
    ping -n 1 192.168.%%G.4
)
:GoTo
@echo off
:A
FOR /L %%G IN (1, 1, 69) DO (
    ping -n 1 192.168.%%G.3 
    ping -n 1 192.168.%%G.4
)
:GoTo
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文