简体   繁体   English

如何将两个.bat文件命令合并为一个文件?

[英]How to merge two .bat file commands to function as one?

Here is what I've been using: 这是我一直在使用的:

PING -n 1 10.0.0.1|find "Reply from" >NUL
IF NOT ERRORLEVEL 1 goto :PASS
IF     ERRORLEVEL 1 goto :FAIL

But this only works if the IP matches. 但这仅在IP匹配的情况下有效。

So, I found these commands on this site (in an answer by Wernfried Domscheit) that seem to work on their own but I don't know how to incorporate them together: 因此,我在此站点上找到了这些命令(Wernfried Domscheit的回答),这些命令似乎可以单独工作,但我不知道如何将它们整合在一起:

In a Batch file: 在批处理文件中:

for /f "tokens=2 delims=:" %%g in ('netsh interface ip show address ^| findstr "Default Gateway"') do ping %%g

At the cmd prompt: cmd提示符下:

for /f "tokens=2 delims=:" %g in ('netsh interface ip show address ^| findstr "Default Gateway"') do ping %g

Any advice? 有什么建议吗?

Instead of checking if the ping output contains "Reply from", it's better to search for "TTL=": 与其检查ping输出是否包含“ Reply from”,不如搜索“ TTL =“:

ping -n 1 10.0.0.1| findstr "TTL="

as pointed in this answer . 如在这个答案中指出的。 Searching for "TTL=" after a ping will be more reliable than searching for "Reply from". ping后搜索“ TTL =”比搜索“ Reply from”更可靠。 There are some ping error messages with "Reply from". 有一些带有“答复自”的ping错误消息。 In some cases you can get a reply from another machine stating that the machine you're looking for is not reachable for example. 在某些情况下,例如,您可能会从另一台计算机收到答复,指出您要查找的计算机无法访问。 You'll then have a "Reply from" in the failed ping output. 然后,您将在失败的ping输出中有一个“答复者”。

If you want to use the ping with the findstr and its errorlevel in a for loop you cannot not use goto to jump to a label. 如果要在for循环中对findstr及其错误级别使用ping,则不能使用goto跳转到标签。 The goto will destroy your loop context. goto将破坏您的循环上下文。 You can see goto as a command that tells the parser to abandon the command it was processing/executing (the FOR loop) and start at the label you gave as argument. 您可以将goto视为命令,该命令告诉解析器放弃它正在处理/执行的命令( FOR循环),并从您作为参数给出的标签开始。 It will just forget about the loop. 它只会忘记循环。

I will assume your labels are arranged this way: 我会假设您的标签是这样排列的:

:PASS
rem some commands in case the ping succeeded
<commands_succes>
goto :BOTH

:FAIL
rem some code in case the ping failed
<commands_fail>

:BOTH
rem code that has to be executed in both cases after code for individual case (if any)
<commands_after>

Your loop can then take these different forms: 然后,循环可以采用以下不同形式:

  • using IF ERRORLEVEL 1 ( ... ) ELSE ( ) and move all code from the :FAIL label to the if-branch and all code from the :PASS label to the else-branch. 使用IF ERRORLEVEL 1 ( ... ) ELSE ( )并将所有代码从:FAIL标签移至if-branch,并将所有代码从:PASS标签移至else-branch。 The code from the :BOTH label (if any) will come under the whole if-block :BOTH标签的代码(如果有的话)将位于整个if块下

     for /f "tokens=2 delims=:" %%g in ('netsh interface ip show address ^| findstr /C:"Default Gateway"') do ( ping %%g | findstr "TTL=">NUL IF ERRORLEVEL 1 ( rem Here comes code in case the ping failed (code for :FAIL label) <commands_fail> ) ELSE ( rem Here comes code in case the ping succeeded (code for :PASS label) <commands_succes> ) rem code that has to be executed in both cases after code for individual case (if any) <commands_after> ) 
  • use function calls with the call command instead of goto . 使用call命令而不是goto函数调用。 If the call command is used with labels it does the same as the goto except that the parser will remember its state (command it was executing) and will come back to it after the execution of the label is exited. 如果将call命令与标签一起使用,则它与goto的功能相同,只是解析器会记住其状态(正在执行的命令),并在退出标签执行后返回到其状态。 so it will execute commands from the label untill an explicit exit , goto :EOF or end of file is encountered. 因此它将执行标签中的命令,直到遇到显式exitgoto :EOF或文件结尾。

     for /f "tokens=2 delims=:" %%g in ('netsh interface ip show address ^| findstr /C:"Default Gateway"') do ( ping %%g | findstr "TTL=" > NUL IF ERRORLEVEL 1 ( rem ping failed call :FAIL ) ELSE ( rem ping succeeded call :PASS ) ) exit /b 0 :PASS rem some commands in case the ping succeeded <commands_succes> goto :BOTH :FAIL rem some code in case the ping failed <commands_fail> :BOTH rem code that has to be executed in both cases after code for individual case (if any) <commands_after> exit /b 0 

Both cases should work for you. 两种情况都应该为您工作。 You'll just have to copy-paste the code corresponding to the <commands_fail> , <commands_success> and <commands_after> on the right places. 您只需要在正确的位置复制粘贴与<commands_fail><commands_success><commands_after>相对应的代码。 If you don't have anything in <commands_after> you can just leave it blank. 如果<commands_after>没有任何内容,则可以将其保留为空白。

EDIT : Thanks to @Josefz who noticed a little mistake I missed in the command you use in the FOR loops to get the IP address of the default gateway: 编辑 :感谢@Josefz,他注意到了我在FOR循环中使用的命令中获取默认网关IP地址时错过的一个小错误:

netsh interface ip show address | findstr "Default Gateway"

The issue is that findstr uses whitespace as delimiter for its regular expressions (whitespace in findstr = | in grep = OR operator). 问题在于, findstr使用空格作为其正则表达式的分隔符(grep = OR运算符中的findstr = |中的空格)。 So what it will be looking for in the case above is Default or Gateway and will not only give you the default gateway but also the gateway metric which isn't even an IP address. 因此,在上述情况下将要查找的是“ Default 或“ Gateway ,它不仅会为您提供默认网关,而且还会为您提供甚至不是IP地址的网关指标。 Use the /C switch of findstr to make it look for litteral string Default Gateway : 使用findstr/C开关查找垃圾字符串Default Gateway

netsh interface ip show address | findstr /C:"Default Gateway"

I've corrected it in the code samples above as well. 我也在上面的代码示例中对其进行了更正。

Good luck! 祝好运!

PS: In both cases you can use conditional execution instead of checking the errorlevel. PS:在两种情况下,您都可以使用条件执行而不是检查错误级别。 Follow the link for more info but be sure to read the note also! 点击链接以获取更多信息,但也请务必阅读说明!

Although not clearly stated, i'll assume that you want to use the IP found by your second command in the first one. 尽管没有明确说明,但我假设您要使用第一个命令中第二个命令找到的IP。 That's simply: 就是这样:

for /f "tokens=2 delims=:" %%g in ('netsh interface ip show address ^| findstr "Default Gateway"') do SET IP=%%g
PING -n 1 %IP%|find "Reply from" >NUL
IF NOT ERRORLEVEL 1 goto :PASS
IF     ERRORLEVEL 1 goto :FAIL

You set a variable ( IP ) in the for statement, and use it instead of the fixed ip in the ping . 您可以在for语句中设置一个变量( IP ),并在ping使用它代替固定的ip。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM