0

I'm trying to ping about ~20-30 servers that are on a text file and can be updated accordingly (servers change name constantly or become obsolete). And I don't want to put the servers in the batch file and have to edit it every time something changes.

But my inquiry would be: how do I ping a set of servers from a .txt file and output the results (if it's online or not) on a separate .txt file (let's call it "Site_A_Servers.txt") with:

Site_A_Servers.txt:
Server A is online.
Server B is online.
Server C is offline!
Server D is etc..

Thank you for your time! :)

Semedar
  • 35
  • 1
  • 2
  • 10

3 Answers3

5

This uses the errorlevel set by ping.exe

@echo off
del log.txt 2>nul
for /f "delims=" %%a in (servers.txt) do ping -n 2 %%a >nul && ( 
>>log.txt echo server %%a is online&echo %%a online) || ( 
>>log.txt echo server %%a is OFFLINE&echo %%a OFFLINE)
foxidrive
  • 40,353
  • 10
  • 53
  • 68
  • I think this is the best answer i would change the script slightly with ^ or enter after the ( and before the ) to break it into three lines but that's just nitpicking – joojaa Jun 04 '13 at 07:57
3
@echo off
(for /F "delims=" %%a in (ServersList.txt) do (
   for /F %%b in ('ping -n 1 "%%a" ^| find /I "TTL="') do set reply=%%b
   if defined reply (
      echo Server %%a is online.
   ) else (
      echo Server %%a is offline!
   )
)) > Site_A_Servers.txt

EDIT: New version added.

The version below use the ERRORLEVEL returned from ping command, as suggested by Joey.

@echo off
setlocal EnableDelayedExpansion
(for /F "delims=" %%a in (ServersList.txt) do (
   ping -n 1 "%%a" > NUL
   if !errorlevel! equ 0 (
      echo Server %%a is online.
   ) else (
      echo Server %%a is offline!
   )
)) > Site_A_Servers.txt
Aacini
  • 65,180
  • 12
  • 72
  • 108
  • Excellent, but ***what does it do***? – BDM Jun 04 '13 at 05:39
  • For each server name in the list: execute `ping ...` with the name. If the server replies then there is a line with `TTL=value`, otherwise not. So just use such reply to know if the server is online. – Aacini Jun 04 '13 at 06:30
  • You can just use the exit code of `ping` instead of relying on strings that may or may not appear depending on the language. – Joey Jun 04 '13 at 06:41
1

You can write a VB script to do this.

Set objFS = CreateObject("Scripting.FileSystemObject")
strFile = "c:\test\ipList.txt"
strTemp = "c:\test\ip_testOP.txt"
Set objFile = objFS.OpenTextFile(strFile)
Set objOutFile = objFS.CreateTextFile(strTemp,True)    
Do Until objFile.AtEndOfStream
    strLine = objFile.ReadLine

    objOutFile.Writeln(Ping(strLine))
Loop
objOutFile.Close
objFile.Close
objFS.DeleteFile(strFile)
objFS.MoveFile strTemp,strFile 


Function Ping(strHost)
    Dim oPing, oRetStatus, bReturn
    Set oPing = GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery("select * from Win32_PingStatus where address='" & strHost & "'")

    For Each oRetStatus In oPing
        If IsNull(oRetStatus.StatusCode) Or oRetStatus.StatusCode <> 0 Then
            bReturn = False

            ' WScript.Echo "Status code is " & oRetStatus.StatusCode
        Else
            bReturn = True

            ' Wscript.Echo "Bytes = " & vbTab & oRetStatus.BufferSize
            ' Wscript.Echo "Time (ms) = " & vbTab & oRetStatus.ResponseTime
            ' Wscript.Echo "TTL (s) = " & vbTab & oRetStatus.ResponseTimeToLive
        End If
        Set oRetStatus = Nothing
    Next
    Set oPing = Nothing

    Ping = bReturn
End Function

Sources:

http://larsmichelsen.com/vbs/quickie-how-to-ping-a-host-in-vbs-i-got-two-ways/

Read and write into a file using VBScript

Community
  • 1
  • 1
RGV
  • 732
  • 3
  • 10