Ping (WMI & Non-WMI Versions) - Functions & Simple Connectivity Monitor

Author Message
dm_4ever

  • Total Posts : 3613
  • Scores: 78
  • Reward points : 0
  • Joined: 6/29/2006
  • Location: Orange County, California
  • Status: offline
Ping (WMI & Non-WMI Versions) - Functions & Simple Connectivity Monitor - Friday, January 26, 2007 1:04 PM ( #1 )
So I've seen a few requests lately concerning the ability to ping to check for connectivity within a vbscript.  Most implementations I've seen call ping.exe using the exec method of wscript.shell and read the output via stdout.  While this works just fine, I think using WMI to ping is cleaner and easier to work with.  There is no need to read the output and try to extract what you want from a string like you do when using ping.exe.  I hope some find this useful and if you have other Ping related ideas, let me know.

The Reachable function will return True or False depending on whether the address provided is reachable or not. The address can be provided with an IP address or Computer Name
The
ResolveIP function will return an IPAddress that corresponds to the Computer Name you provide

Added 1/29/07 - I put together a simple "Connectivity Monitor" using Win32_PingStatus (I've attached a screen shot of what it looks like). You can download it from here (I put it in a zip because of the images used for it): ConnectivityMonitor

Reference for this class: http://msdn2.microsoft.com/en-us/library/aa394350.aspx
NOTE: WMI is NOT required on the host being ping'd.

Reachable Function (WinXP/Win2k3)
 '==========================================================================
 ' The following function will test if a machine is reachable via a ping
 ' using WMI and the Win32_PingStatus Class
 '==========================================================================
 If Reachable("10.50.138.48") Then
  WScript.Echo "Computer is Reachable!"
 Else 
  WScript.Echo "Computer is Unreachable!"
 End If
 
 Function Reachable(strComputer)
 '     On Error Resume Next
 
  Dim wmiQuery, objWMIService, objPing, objStatus
  
  wmiQuery = "Select * From Win32_PingStatus Where Address = '" & strComputer & "'"
  
  Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
  Set objPing = objWMIService.ExecQuery(wmiQuery)
  
  For Each objStatus in objPing
      If IsNull(objStatus.StatusCode) Or objStatus.Statuscode<>0 Then
          Reachable = False 'if computer is unreacable, return false
      Else
          Reachable = True 'if computer is reachable, return true
      End If
  Next
 End Function
 


Reachable Function - Non WMI Version (Win2k/WinXP/Win2k3)
 If Reachable("10.50.138.48") Then
  WScript.Echo "Computer is Reachable!"
 Else
  WScript.Echo "Computer is Unreachable!"
 End If
 
 Function Reachable(strComputer)
 '     On Error Resume Next
 
  Dim objShell, objExec, strCmd, strTemp
  
  strCmd = "ping -n 1 " & strComputer
  
  Set objShell = CreateObject("WScript.Shell")
  Set objExec = objShell.Exec(strCmd)
  strTemp = UCase(objExec.StdOut.ReadAll)
  
  If InStr(strTemp, "REPLY FROM") Then
   Reachable = True 
  Else
   Reachable = False
  End If
 End Function
 


ResolveIP Function (WinXP/Win2k3)
 '==========================================================================
 ' The following function will resolve a computer name to its ip address
 ' using WMI and the Win32_PingStatus Class
 '==========================================================================
 WScript.Echo ResolveIP("HOSTNAME")
 
 Function ResolveIP(strComputer)
 Dim wmiQuery : wmiQuery = "Select * From Win32_PingStatus Where Address = '" & strComputer & "'"
 
 Dim objWMIService : Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
 Dim objPing : Set objPing = objWMIService.ExecQuery(wmiQuery)
 Dim objStatus
 For Each objStatus in objPing
    If IsNull(objStatus.StatusCode) Or objStatus.Statuscode<>0 Then
        ResolveIP = "Computer is Unreachable!"
    Else
        ResolveIP = objStatus.ProtocolAddress
    End If
 Next
 End Function
 


ResolveIP Function (Win2k/WinXP/Win2k3)
 WScript.Echo ResolveIP("Computer1")
 
 Function ResolveIP(computerName)
     Dim objShell  :  Set objShell = CreateObject("WScript.Shell")
     Dim objExec   :  Set objExec = objShell.Exec("ping " & computerName & " -n 1")
     Dim strOutput : strOutput = objExec.StdOut.ReadAll
     Dim RegEx     :  Set RegEx = New RegExp
     RegEx.Pattern = "\[(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\]"
     RegEx.Global = True
     If RegEx.Test(strOutput) Then
         ResolveIP = RegEx.Execute(strOutput)(0).Submatches(0)
     Else
         ResolveIP = "IP Address could not be resolved."
     End If
 End Function
 


[image]local://11180/2BF856EFE52B457E953D3AD42CE190F2.jpg[/image]
<message edited by dm_4ever on Monday, May 19, 2008 8:48 AM>
Attachments are not available: Download requirements not met - - -
dm_4ever

My philosophy: K.I.S.S - Keep It Simple Stupid
Read Me: http://www.visualbasicscript.com/m_24727/tm.htm
Frequently Asked Stuff: http://www.visualbasicscript.com/m_47117/tm.htm
dm_4ever

  • Total Posts : 3613
  • Scores: 78
  • Reward points : 0
  • Joined: 6/29/2006
  • Location: Orange County, California
  • Status: offline
RE: Ping (WMI & Non-WMI Versions) - Functions & Simple Connectivity Monitor - Tuesday, February 27, 2007 3:16 PM ( #2 )
Added a Non-WMI Reachable Ping Function for those using Win2k.
dm_4ever

My philosophy: K.I.S.S - Keep It Simple Stupid
Read Me: http://www.visualbasicscript.com/m_24727/tm.htm
Frequently Asked Stuff: http://www.visualbasicscript.com/m_47117/tm.htm
mcds99

  • Total Posts : 503
  • Scores: 4
  • Reward points : 0
  • Joined: 2/28/2006
  • Status: offline
RE: Ping (WMI & Non-WMI Versions) - Functions & Simple Connectivity Monitor - Thursday, March 29, 2007 1:57 AM ( #3 )
The Connectivity Monitor is SO COOL!

I love scripts that have KISMIF!

(Keep It Simple Make It Fun!)
Sam

Keep it Simple Make it Fun KiSMiF
dm_4ever

  • Total Posts : 3613
  • Scores: 78
  • Reward points : 0
  • Joined: 6/29/2006
  • Location: Orange County, California
  • Status: offline
RE: Ping (WMI & Non-WMI Versions) - Functions & Simple Connectivity Monitor - Friday, March 30, 2007 1:30 PM ( #4 )
  I'm glad you liked it.
dm_4ever

My philosophy: K.I.S.S - Keep It Simple Stupid
Read Me: http://www.visualbasicscript.com/m_24727/tm.htm
Frequently Asked Stuff: http://www.visualbasicscript.com/m_47117/tm.htm
4scriptmoni

  • Total Posts : 225
  • Scores: 0
  • Reward points : 0
  • Joined: 5/3/2007
  • Status: offline
RE: Ping (WMI & Non-WMI Versions) - Functions & Simple Connectivity Monitor - Tuesday, May 08, 2007 2:54 AM ( #5 )
Amazing job!!!
Great GUI, I also want to try to make an .hta. Also checked your site, I will make a link to it from mine..

regards,
Enterprise Microsoft Scripts Exchange, Login/Logout Monitor,TS, Monitoring, Security, AD, etc... http://www.felipeferreira.net
guitarguy2576

  • Total Posts : 13
  • Scores: 0
  • Reward points : 0
  • Joined: 7/14/2007
  • Status: offline
RE: Ping (WMI & Non-WMI Versions) - Functions & Simple Connectivity Monitor - Sunday, August 19, 2007 12:18 PM ( #6 )
Just wondering.... is a ping really the best way to do this? I'm just wondering how the connectivity for a computer is checked by an OS. Does it literally ping every single computer before showing you which ones are available? If it doesn't... then isn't there some way to tap into that information source to show a faster result for all the computer in a network.
I've been working (off and on) to make something that checks a specific OU's list of computers and then makes sure that they are online before listing them. From there, I would be able to get an accurate list of the computers I needed to send a certain installation file to a startup folder etc. Our active directory is filled with outdated computers, and nobody has enough time to sift through them all...... yet. Any ideas on computer verification???
dm_4ever

  • Total Posts : 3613
  • Scores: 78
  • Reward points : 0
  • Joined: 6/29/2006
  • Location: Orange County, California
  • Status: offline
RE: Ping (WMI & Non-WMI Versions) - Functions & Simple Connectivity Monitor - Sunday, August 19, 2007 2:32 PM ( #7 )
If your AD has a lot of outdated computer records then it sounds like you may want work  on some clean up scripts.  Each computer would have a "LastLogonTimeStamp" value...the thing with that is that you would have to query all domain controllers since I don't believe this information gets replicated. 

As for pinging...it is a basic connectivity check.  http://en.wikipedia.org/wiki/Ping

As for speed...controlling and initiating several scripts to do the ping and write it to something like a database may be a bit faster.

Depending on the size of your organization...you should consider investing into a system like MS SMS to deploy software or make changes.

You might also consider deploying a login script that will gather a computers information and write it to a centralized database of some sort.
dm_4ever

My philosophy: K.I.S.S - Keep It Simple Stupid
Read Me: http://www.visualbasicscript.com/m_24727/tm.htm
Frequently Asked Stuff: http://www.visualbasicscript.com/m_47117/tm.htm
guitarguy2576

  • Total Posts : 13
  • Scores: 0
  • Reward points : 0
  • Joined: 7/14/2007
  • Status: offline
RE: Ping (WMI & Non-WMI Versions) - Functions & Simple Connectivity Monitor - Sunday, August 19, 2007 3:12 PM ( #8 )
Definitely good insight....
It wasn't so much that I was against pinging as it is the fact that our company has a weird setup. We got bought out by another company a few years ago, and they have basically taken over our domain. We have our own little "nook", but they still are the end all be all when it comes to monitoring the entire domain. Since they monitor the network for pings that might represent harmful worms etc, I run the risk of my network connection being shut down when I'm just trying to populate one of our larger branches list of computers.
I do need to look into querying the last login for our computers. Quite honestly, I'm just trying to get a grasp on all the things you can do with scripting. I wish some of this stuff would have been taught to me in college instead of a lot of the useless "variety programming" I was put through. I'm getting certain concepts, but as time goes on, I can see how in depth it all can get. Thanks for the suggestions and help though... This is one of the most useful sites I've come across.
<message edited by guitarguy2576 on Monday, August 20, 2007 11:52 AM>
CondoPC

  • Total Posts : 118
  • Scores: 0
  • Reward points : 0
  • Joined: 7/23/2007
  • Status: offline
RE: Ping (WMI & Non-WMI Versions) - Functions & Simple Connectivity Monitor - Monday, August 20, 2007 8:58 AM ( #9 )
If you are looking at validating your records pulled from AD, then lastlogin property of the AD record would work. You can also compare that to DNS records and DHCP lease time to get a filtered list of known computer accounts that dhave been on the network in x days (based on your DNS and DHCP record scavenging).
Once you build a filtered list, you only need to keep it updated on a schedule. Then use your dropdown to validate the accounts between AD and your filtered list. That would eliminate your need for ping. Your records may not be 100% accurate, but you at least would not have any records older than your update interval.
wsmoth

  • Total Posts : 3
  • Scores: 0
  • Reward points : 0
  • Joined: 8/22/2007
  • Status: offline
RE: Ping (WMI & Non-WMI Versions) - Functions & Simple Connectivity Monitor - Wednesday, August 22, 2007 5:46 AM ( #10 )
dm,
Great GUI, nice start but a basic connectivity test only tells you if the server is up, it does not tell you if all the services are up and responding and if the shares are available...

I have a Kixtart script that takes this concept a bit further but it does not have a GUI.

Here is the Kix Script:
 Break On
 CLS
 
 Del '%windir%\RebootCheckError.log'
 Del '%Windir%\RebootCheck.log'
 Del '%windir%\RebootCheckServices.log'
 $Error           = "N"
 $LoggedErrorOnce = "N"
 
 ;**********************
 ;
 ;      Main
 ;
 ;**********************
 :Main
 If Open(1,"c:\servers.txt") = 0
   $Server = ReadLine(1)
   
   While @ERROR = 0
     $Continue = "Y"
     $LogText  = "@CRLF------------------------------------------------------------@CRLFChecking $Server started @Date @Time@CRLF------------------------------------------------------------"
  
     Gosub LogIt
     Gosub PingTest
  
     If $Continue = "Y"
       Gosub NetView
     EndIf
  
     If $Continue = "Y"
       Gosub Uptime
     EndIf
  
     If $Continue = "Y"
       Gosub CheckServices
     EndIf
  
     $LoggedErrorOnce = "N"
     $Server = ReadLine(1)
   Loop
   
   $CloseFile = Close(1)
 
 EndIf
 Gosub Results
 Quit
 
 
 ;**********************
 ;
 ;     Ping Test
 ;
 ;**********************
 :PingTest
 ? "Pinging $Server....@CRLF@CRLF"
 Shell '%Comspec% /c Ping $Server -n 3 |find /I "Reply from"'
 
 If @ERROR = 0
   $LogText  = "PING RESULT     - Ping successful"
   $Continue = "Y"
 Else
   If $LoggedErrorOnce = "N"
     Shell '%Comspec% /c Echo $Server - Ping failed.>>%windir%\RebootCheckError.log'
     $LoggedErrorOnce = "Y"
   EndIf
   $Error    = "Y"
   $LogText  = "PING RESULT - Ping failed."
   $Continue = "N"
 EndIf
 
 Gosub LogIt
  
 Return
 
 ;**********************
 ;
 ;    Net View Test
 ;
 ;**********************
 :NetView
 ? "@CRLF@CRLFNet viewing $Server...@CRLF@CRLF"
 Shell '%Comspec% /c Net View \\$Server'
 If @ERROR = 0
   $LogText  = "NET VIEW RESULT - Net View successful"
   $Continue = "Y"
 Else
   If $LoggedErrorOnce = "N"
     Shell '%Comspec% /c Echo $Server - Net view failed>>%windir%\RebootCheckError.log'
     $LoggedErrorOnce = "Y"
   EndIf
   $Error    = "Y"
   $LogText  = "NET VIEW RESULT - Net view failed"
   $Continue = "N"
 EndIf 
 Gosub LogIt
 
 Return
 
 ;**********************
 ;
 ;     Uptime
 ;
 ;**********************
 :Uptime
 ? "@CRLF@CRLFQuerying uptime for $Server...@CRLF"
 $LogText = "UPTIME RESULT   - "
 Gosub LogIt
 Shell '%Comspec% /c Uptime >%windir%\RebootCheck.Log'">\\$Server>>%windir%\RebootCheck.Log'
 Shell '%Comspec% /c Uptime \\$Server'
 Return
 
 ;**********************
 ;
 ;   Check Services
 ;
 ;**********************
 :CheckServices
 $objWMIService = GetObject("winmgmts:\\"+ $Server + "\root\cimv2")
 $colItems      = $objWMIService.ExecQuery("Select * from Win32_Service")
 
 For Each $objItem in $colItems
      
   ? "Checking Service - " + $objItem.Name
   
   If $objItem.StartMode = "Auto"
     ? "  " + $objItem.Name + " is set to auto, querying status..."
  
     If $objItem.State <> "Started" And $objItem.State <> "Running"
       $LogText = "SERVICE CHECK   - " + $objItem.DisplayName + " - NOT STARTED!!!"
       Gosub LogIt
       $Error   = "Y"
       Shell '%Comspec% /c Echo $Server - SERVICE ' + $objItem.DisplayName + ' - NOT STARTED!!!>>%windir%\RebootCheckError.log'
     Else
       ;   $LogText = "  Started - "+ $objItem.DisplayName 
     EndIf
    
   Else
 
     ? "  " + $objItem.Name + " not set to auto, skipping."
  
   EndIf
  
 
 Next
 Return
 
 ;**********************
 ;
 ;     Results
 ;
 ;**********************
 :Results
 If $Error = "Y"
   $Msg = MessageBox("One or more errors were encountered on one or more servers.@CRLF@CRLFTwo log files have been created:@CRLF - RebootCheck.Log - Contains a list of all output from tests.@CRLF - RebootCheckError.Log - A list of servers that an error was produced on@CRLF@CRLFLogs are located at %windir%\RebootCheck.log and %windir%\RebootCheckError.log.","Errors encountered.",48)
   Run '%Comspec% /c Notepad %windir%\RebootCheck.log'
   Run '%Comspec% /c Notepad %windir%\RebootCheckError.log'
 
 Else
   $Msg = MessageBox("No errors were encountered. Opening %windir%\RebootCheck.log.","No Errors",64)
   Run '%Comspec% /c Notepad %windir%\RebootCheck.log'
 EndIf
 Return
 
 ;**********************
 ;
 ;     Logging
 ;
 ;**********************
 :LogIt
 $Log = RedirectOutput("%Windir%\RebootCheck.log")
  ? "$LogText"
 $Log = RedirectOutput("")
 ? "$LogText"
 Return
 

EDIT://Placed code in code blocks - dm_4ever

If you could accomplish something along these same lines with that beautiful interface, I think you would have a really winner. I am not skilled enough with vb to convert this Kix to vb but if you wanted to convert it, and then add your great GUI to it, I would greatly apprecaite it!

Thanks,
Will
<message edited by dm_4ever on Wednesday, August 22, 2007 4:03 PM>
dm_4ever

  • Total Posts : 3613
  • Scores: 78
  • Reward points : 0
  • Joined: 6/29/2006
  • Location: Orange County, California
  • Status: offline
RE: Ping (WMI & Non-WMI Versions) - Functions & Simple Connectivity Monitor - Wednesday, August 22, 2007 4:17 PM ( #11 )
I really don't have a need for a VBScript version of the KIX script you posted and I already know how to do everything that is required so there is not much to be gained on my part.

Read/Write to a text file...do a search for FileSystemObject and the OpenTextFile method:  http://msdn2.microsoft.com/en-us/library/z9ty6h50.aspx & http://msdn2.microsoft.com/en-us/library/314cz14s.aspx
Ping...well you have plenty of examples in this post
Executing Net View and UpTime...do a search for WSHSHell and the Run method:  http://msdn2.microsoft.com/en-us/library/d5fk67ky.aspx
Checking Services...the example you posted is already using WMI and the code would be almost the same without the $

It is not that I don't think the script would be useful to others or that I don't want to help...helping those who are willing to put forth an effort to do it on their own is what many of us look for.  Perhaps you will get lucky and someone will provide you with what you need, but if you have other scripts to convert you might as well start learning and doing this on your own...as much as possible of course.  If you have specific questions about something or an error...feel free to post in the WSH & Client Side VBScript forum.
dm_4ever

My philosophy: K.I.S.S - Keep It Simple Stupid
Read Me: http://www.visualbasicscript.com/m_24727/tm.htm
Frequently Asked Stuff: http://www.visualbasicscript.com/m_47117/tm.htm
pnasim

  • Total Posts : 7
  • Scores: 0
  • Reward points : 0
  • Joined: 7/24/2008
  • Status: offline
RE: Ping (WMI & Non-WMI Versions) - Functions & Simple Connectivity Monitor - Thursday, July 24, 2008 1:43 AM ( #12 )
good one

After experiencing a lot of down time, We decided to move this site to CrystalTech.com. CrystalTech.com is powered by only the finest Windows servers providing the best performance, reliability, and value anywhere.
Jump to:

Current active users
There are 0 members and 1 guests.
Icon Legend and Permission
  • New Messages
  • No New Messages
  • Hot Topic w/ New Messages
  • Hot Topic w/o New Messages
  • Locked w/ New Messages
  • Locked w/o New Messages
  • Read Message
  • Post New Thread
  • Reply to message
  • Post New Poll
  • Submit Vote
  • Post reward post
  • Delete my own posts
  • Delete my own threads
  • Rate post

© 2000-2009 ASPPlayground.NET Forum Version 3.6