Announcement

Collapse
No announcement yet.

SCRIPT or BAT file to ping a list of computers..please help!

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • SCRIPT or BAT file to ping a list of computers..please help!

    Hi,

    I have a list of computers (in a text/excel file) which I want to ping and verify the existence. They are 500+ host names in different OUs in our AD, and put in different VLANs, since it's hard to find and ping each of it's IP/Hostname....please suggest me a way to make a script or bat file to ping the whole list which gives accurate understandable result.

    I am a novice about scripting and even how to develop one.

    OR, is there any ping tool to import a list of computers and hit?
    Insaf Muhammed
    System Admin
    -----------------
    Never break four things in life: TRUST, PROMISE, RELATIONS & HEART. Cause when they break they don't make noise but pains a lot

  • #2
    Re: SCRIPT or BAT file to ping a list of computers..please help!

    hey

    first save the code as vbs file then
    edit the path for the list file and the log file as you wish in line 1 and 2

    Code:
     strList = "c:\list.txt"
    strLog = "c:\log.txt"
    
    Set objfso = CreateObject("scripting.filesystemobject")
    Set objlist = objfso.OpenTextFile(strList,1)
    Set objlog = objfso.CreateTextFile(strLog,True)
    
    DoUntil objlist.AtEndOfStream
    strComputer = objlist.ReadLine
    objlog.WriteLine Ping(strComputer) 
    Loop
    
    
    
    Function Ping(strComputer)
    Set objShell = CreateObject("WScript.Shell")
    Set objScriptExec = objShell.Exec( _
        "ping -n 2 -w 1000 " & strComputer)
    strPingResults = LCase(objScriptExec.StdOut.ReadAll)
    IfInStr(strPingResults, "reply from") Then
        IfInStr(strPingResults, "destination net unreachable") Then
            Ping = "did not respond to ping."
        Else
            Ping = strComputer & " responded to ping."
        EndIf
    Else
        Ping = strComputer & " did not respond to ping."
    EndIf
    EndFunction
    
    Any advice is given in good faith and without warranty.
    Please give reputation points where appropriate.

    Comment


    • #3
      Re: SCRIPT or BAT file to ping a list of computers..please help!

      Hi largo, thank you very much for the stuff.

      But when I copied the code onto a notepad it appeared little weird different than it is on the box you posted (with unknown marks ans signs between the commands). I doubt the code gets messed up may be because simple notepad does not support it ??

      Is there a better app to create/save a vbs file? (I heard something like Notepad++)

      Advise
      Insaf Muhammed
      System Admin
      -----------------
      Never break four things in life: TRUST, PROMISE, RELATIONS & HEART. Cause when they break they don't make noise but pains a lot

      Comment


      • #4
        Re: SCRIPT or BAT file to ping a list of computers..please help!

        It would be the first time that Notepad messed things up.
        Personally I use Notepad++ for the most of the time.
        Marcel
        Technical Consultant
        Netherlands
        http://www.phetios.com
        http://blog.nessus.nl

        MCITP(EA, SA), MCSA/E 2003:Security, CCNA, SNAF, DCUCI, CCSA/E/E+ (R60), VCP4/5, NCDA, NCIE - SAN, NCIE - BR, EMCPE
        "No matter how secure, there is always the human factor."

        "Enjoy life today, tomorrow may never come."
        "If you're going through hell, keep going. ~Winston Churchill"

        Comment


        • #5
          Re: SCRIPT or BAT file to ping a list of computers..please help!

          In notepad select and copy one of the "squares" (unmapped characters) then use notepad's Replace feature to replace each "square" with a space, use 'Replace All'.


          Note! - the actual text returned from Ping.exe is OS-language depended! So it might be better to replace
          Code:
          
          strPingResults = LCase(objScriptExec.StdOut.ReadAll)
          If InStr(strPingResults, "reply from") Then
              If InStr(strPingResults, "destination net unreachable") Then
                  Ping = "did not respond to ping."
              Else
                  Ping = strComputer & " responded to ping."
              End If 
          Else
              Ping = strComputer & " did not respond to ping."
          End If
          
          with just:
          Code:
          
          strPingResults = objScriptExec.StdOut.ReadAll
          If InStr(1, strPingResults, "ttl=",1) Then
             Ping = strComputer & " responded to ping."
          Else
              Ping = strComputer & " did not respond to ping."
          End If
          


          Or.. here is an other sample,
          Code:
          Const ForReading = 1
          
          strList = "c:\folder\computers.txt"
          strLog = "c:\folder\computers.log"
          
          Dim objWMIService
          Set objWMIService = GetObject("winmgmts:{impersonationLevel=" _
             & "impersonate}!\\.\root\cimv2")
          
          Set objfso = CreateObject("scripting.filesystemobject")
          Set objlist = objfso.OpenTextFile(strList, ForReading)
          Set objlog = objfso.CreateTextFile(strLog, True)
          
          objlog.WriteLine now()
          objlog.WriteLine
          
          Do Until objlist.AtEndOfStream
             objlog.WriteLine ProtocolAddress(objlist.ReadLine)
          Loop
          objlist.close
          objlog.close
          
          wscript.echo "Done"
          
          Function ProtocolAddress(sHost)
             Set colPings = objWMIService.ExecQuery _
              ("Select * From Win32_PingStatus where Address = '" & sHost & "'")
             For Each objPing in colPings
               If objPing.StatusCode = 0 Then
                 ProtocolAddress = sHost & ", " & objPing.ProtocolAddress
               Else
                 ProtocolAddress = sHost & ", <unreachable>"
               End If
             Next
          End Function
          Personally I'm fond of Notepad2.exe myself (which is also free for download), the tool is light and simple.


          \Rems
          Last edited by Rems; 27th April 2010, 20:31.

          This posting is provided "AS IS" with no warranties, and confers no rights.

          __________________

          ** Remember to give credit where credit's due **
          and leave Reputation Points for meaningful posts

          Comment


          • #6
            Re: SCRIPT or BAT file to ping a list of computers..please help!

            Thanks to dumber, largo and rems

            Actually I got these 'squares' between the commands which were what I meant by weird signs :P, it's okey now once replaced.

            The script is doing the ping job over 600+ host names now, the log is keeping on growing hopefully with appropriate result .....hooolallaaaa


            Thanks for your valuable support guys..
            Insaf Muhammed
            System Admin
            -----------------
            Never break four things in life: TRUST, PROMISE, RELATIONS & HEART. Cause when they break they don't make noise but pains a lot

            Comment

            Working...
            X