批处理文件以运行ping命令并输出到文本 [英] Batch file to run ping command and output to text aswell

查看:815
本文介绍了批处理文件以运行ping命令并输出到文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个批处理文件来ping文本文件servers.txt中的所有IP. 批处理文件之一对服务器执行ping操作,并在CMD窗口中显示结果. 另一个批处理文件对服务器执行ping操作,并在CMD窗口上不显示任何内容,并在完成对服务器的所有ping操作后,会将它们放入OnlineServers.txt文件中.

I have two batch files to ping all IPs in a text file servers.txt. One of the batch files pings the servers and shows the result on the CMD window. The other batch file pings the servers and shows nothing on CMD windows and after it finished all the pings to servers , it will put them in the OnlineServers.txt file.

我想把这东西混起来.

我希望批处理文件运行ping并在屏幕上显示它们,然后将所有在线服务器放在OnlineServers.txt中.

I want the batch file to run pings and show them on screen and put all the online servers in the OnlineServers.txt .

这是第一个批处理文件,它显示CMD窗口上的ping命令,而没有输出到文本文件:

Here is first batch file which shows pings on CMD windows without any output to text file :

  @echo off
  for /f "delims=" %%a in (servers.txt) do ping -n 1 %%a >nul && (echo %%a      Online) || (echo %%a        Offline)

 echo.
 pause

这是第二个批处理文件,它在CMD窗口上不显示任何内容,仅在ping所有服务器后才输出该文件:

And here is the second batch file which shows nothing on CMD window and only output the file after it pings all the servers :

 @echo off
 setlocal EnableDelayedExpansion
 (for /F "delims=" %%a in (servers.txt) do (
 ping -n 1 "%%a" > NUL
 if !errorlevel! equ 0 (
  echo %%a      Online
 )
 )) > OnlineServers.txt

这些要检查的服务器超过150台,我每天都将服务器添加到此列表中,因此要检查的列表很长.

These is more than 150 servers to check and I add servers to this list every day so its a long list to check.

推荐答案

批量写入文件将不得不中断括号. 重定向,并在开始时重置文件.

The batch writing to the file will have to breakoff the enclosing parentheses with the redirection and reset the file at start.

@echo off
setlocal EnableDelayedExpansion

:: Reset file
Type Nul >OnlineServers.txt

for /F "delims=" %%a in (servers.txt) do (
    ping -n 1 "%%a" >NUL 2>&1
    If !errorlevel! equ 0 (
       echo %%a      Online
       echo %%a      Online>>OnlineServers.txt
    ) Else (
       echo %%a      Offline
    )
)

这篇关于批处理文件以运行ping命令并输出到文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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