Sometimes it can come in handy to actually check, instead of just assume, that a destination is available before you let your script loose. One method you can use for this is ping. I recently wrote a log on script which pings a server before it attempts to map a network drive.
That script used a very simple, but yet effective, function that returned true or false based on the result. If ping failed (the function returned false), it would continue to try for 90 seconds before giving up. Here’s that little piece of code:
Function Ping(address)
Ping = False
Set objWMI = GetObject("winmgmts:\\.\root\cimv2")
Set objPing = objWMI.Get("Win32_PingStatus.Address='" & address & "'")
If objPing.StatusCode = 0 Then
Ping = True
End If
End Function
WScript.Echo Ping("google.com")
To test this function, save it in a text file with the file extension .vbs, and run the file. It should return either –1 (true) or 0 (false) to the screen.
But perhaps you want a bit more information than that, since the above script was not made for human readability, but rather to be used by other functions within the script.
So I put together two more functions for this purpose. PingStatusCode and PingStatus. The former will get the return code and the latter will translate the code to text, when it is passed to it.
Function PingStatusCode(address)
Set objWMI = GetObject("winmgmts:\\.\root\cimv2")
Set objPing = objWMI.Get("Win32_PingStatus.Address='" & address & "'")
PingStatusCode = objPing.StatusCode
End Function
Function PingStatus(PingStatusCode)
Select Case Int(PingStatusCode)
Case Int(0)
PingStatus = "Success"
Case Int(11001)
PingStatus = "Buffer Too Small"
Case Int(11002)
PingStatus = "Destination Net Unreachable"
Case Int(11003)
PingStatus = "Destination Host Unreachable"
Case Int(11004)
PingStatus = "Destination Protocol Unreachable"
Case Int(11005)
PingStatus = "Destination Port Unreachable"
Case Int(11006)
PingStatus = "No Resources"
Case Int(11007)
PingStatus = "Bad Option"
Case Int(11008)
PingStatus = "Hardware Error"
Case Int(11009)
PingStatus = "Packet Too Big"
Case Int(11010)
PingStatus = "Request Timed Out"
Case Int(11011)
PingStatus = "Bad Request"
Case Int(11012)
PingStatus = "Bad Route"
Case Int(11013)
PingStatus = "TimeToLive Expired Transit"
Case Int(11014)
PingStatus = "TimeToLive Expired Reassembly"
Case Int(11015)
PingStatus = "Parameter Problem"
Case Int(11016)
PingStatus = "Source Quench"
Case Int(11017)
PingStatus = "Option Too Big"
Case Int(11018)
PingStatus = "Bad Destination"
Case Int(11032)
PingStatus = "Negotiating IPSEC"
Case Int(11050)
PingStatus = "General Failure"
Case Else
PingStatus = "Unknown Error"
End Select
End Function
You can test the functions by adding these lines of code, outside the functions, before you run the script:
WScript.Echo PingStatusCode("google.com") & vbTab & PingStatus(PingStatusCode("google.com"))
WScript.Echo PingStatusCode("microsoft.com") & vbTab & PingStatus(PingStatusCode("microsoft.com"))
The script will first ping google.com and give you the return code and message, then it will ping microsoft.com and, again, give you the return code and message.
Here’s the MSDN reference for the Win32_PingStatus Class:
http://msdn.microsoft.com/en-us/library/aa394350(v=VS.85).aspx
Thanks for Sharing a Nice Article.
ReplyDeleteThanks,
Ganesan K
My Site
Internet ialah satu media yang sekarang telah sangatberkembang serta banyak digunakan oleh kebanyakan orang untuk memudahkan semua aktivitasnya. Kehadiran internet yang telah memang dibuka oleh kebanyakan orang membuat Agen SBOBET Terpercaya tidak melepaskan kesempatan kali ini dengan membuat service situs penyuplai permainan judi di (Baca Selengkapnya Disini...)
ReplyDelete