批处理文件 - 环平 - 输出到文件中均达到主机 [英] Batch file - loop ping - output to file hosts that are up

查看:186
本文介绍了批处理文件 - 环平 - 输出到文件中均达到主机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想作一个.bat文件,会做一个for循环象下面这样:

 关闭@echo
FOR / L %%克由(1,1,69)DO(
    平-N 1 192.168 %% G.3
    平-N 1 192.168 %% G.4

然后再看看通过输出和仅发送成功回答了ping到一个txt文件中的IP。这可能与一个CMD批处理文件?


解决方案

  @ECHO OFF
SET输出=%USERPROFILE%\\ output.txt的
如果不存在%输出%DEL%输出%
FOR / L %%克由(1,1,69)DO(
    CALL:平192.168 %% G.3。
    CALL:平192.168 %% G.4。

GOTO:EOF:平
平-n 1%1> NUL&放大器;&安培; ECHO%1>>中%的输出%

基本上,你可以使用&放大器;&安培; 补充说,只有执行的命令,如果previous命令(前一个 &功放;&安培; )成功完成(从技术上来讲,返回的退出code 0

有对相反的情况类似的方法。如果你想在一个命令的失败结果执行一些动作,你把 || 后,再实施你的行动命令。

修改

关于一注平。有时候你会从主机不能访问路由器的通知。在这种情况下仍然 0 code(成功)退出,因为它得到的答复,即使它从路由器而不是从实际的主机。

如果能够与您的主机的情况下,你不希望在输出文件这样的误报,你必须解析平对于某些关键字,指示ping命令是否成功确实如此。到目前为止,您可以依靠显示的汇总统计行:他们仅得到的答复是来自目标主机出现

所以,这里的另一种方法:

  @ECHO OFF
SET输出=%USERPROFILE%\\ output.txt的
如果不存在%输出%DEL%输出%
FOR / L %%克由(1,1,69)DO(
    CALL:平192.168 %% G.3。
    CALL:平192.168 %% G.4。

GOTO:EOF:平
平-n 1%1 |找到大约往返> NUL&放大器;&安培; ECHO%1>>中%的输出%

编辑2

改变这两个解决方案使用子程序调用,以避免%IP%的premature扩张中的循环。 (还可以通过使延迟的扩展,而不是固定的。)

还援引%输出%无处不在。

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?

解决方案

@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.

这篇关于批处理文件 - 环平 - 输出到文件中均达到主机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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