Jan 18

PingHost.vbs

'=========================================================================
' PingHost.vbs
' VERSION: 1.0
' AUTHOR: Brian Steinmeyer
' EMAIL: sigkill@sigkillit.com
' WEB: http://sigkillit.com
' DATE: 1/17/2013
' COMMENTS: Pass a hostname or IP to the function and the number of times
' you wish to ping the host/IP, and it will return whether it is succesful
' or not.
' EXAMPLE: Ping by Hostname
' Dim strHost: strHost = "Server"
' Dim intCount: intCount = 4
' EXAMPLE: Ping by IP
' Dim strHost: strHost = "192.168.1.1"
' Dim intCount: intCount = 4
'=========================================================================
Option Explicit
' ------ SCRIPT CONFIGURATION ------
Dim strHost: strHost = "Server"
Dim intCount: intCount = 4
' ------ END CONFIGURATION ------

Wscript.Echo PingHost(strHost, intCount)

Private Function PingHost(strHostOrIP, count)

	On Error Resume Next ' Start Error Handling

	'Set Variables
	Dim objPing, objRetStatus, i, strResult: strResult = ""

	For i = 1 to count
		Set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery("select * from Win32_PingStatus where address = '" & strHostOrIP & "'")
		For Each objRetStatus in objPing
			If IsNull(objRetStatus.StatusCode) or objRetStatus.StatusCode<>0 Then
				strResult = "!~ERROR~!" & objRetStatus.StatusCode
				'WScript.Echo "Status code is " & objRetStatus.StatusCode
			Else
				strResult = "OK"
				'Wscript.Echo "Bytes = " & vbTab & objRetStatus.BufferSize
				'Wscript.Echo "Time (ms) = " & vbTab & objRetStatus.ResponseTime
				'Wscript.Echo "TTL (s) = " & vbTab & objRetStatus.ResponseTimeToLive
			End If
		Next

		' Error Check
		If Err.Number <> 0 Then
		strResult = "!~ERROR~!"
		End If

		'Check For Success
		If strResult = "OK" Then
			Exit For
		End If
		count = count -1
	Next

	'Return Result
	PingHost = strResult

	On Error GoTo 0 ' End Error Handling

End Function

 

Jan 01

GetIPInfo.vbs

'=========================================================================
' GetIPInfo.vbs
' VERSION: 1.0
' AUTHOR: Brian Steinmeyer
' EMAIL: sigkill@sigkillit.com
' WEB: http://sigkillit.com
' DATE: 1/1/2011
' COMMENTS: Input a Computer DNS/IP, which then Clears the Current Arp Cache,
' Gets the IP of the DNS/IP, and then gets the MAC address of the DNS/IP.
' EXAMPLE: Wscript.Echo GetIPInfo("Computer")
'=========================================================================
Option Explicit
Wscript.Echo GetIPInfo("Computer")

Private Function GetIPInfo(strComputerOrIP)

    On Error Resume Next

    Dim strResult: strResult = ""
    Call ClearARP()
    ' Get IP of Host Name
    Dim strIP: strIP = GetIP(strComputerOrIP)
    ' Get MAC of Host Name
    Dim strArp: strArp = GetMAC(strIP)

    GetIPInfo = strComputerOrIP & vbTab & strIP & vbTab & strArp

    On Error Goto 0

End Function

' **************************************************************************************************
' Sub ClearARP - Clears the ARP Cache
' **************************************************************************************************
Private Sub ClearARP()

        On Error Resume Next

        ' Clear ARP Cache
       Dim objShell: Set objShell = WScript.CreateObject("WScript.Shell")
        objShell.Run("netsh interface ip delete arpcache")

        ' Kill Objects
        Set objShell = Nothing

        On Error Goto 0

End Sub

' **************************************************************************************************
' Function GetIP - Returns an IP From Passing an Host Name or IP Into the Function
' ********************************************************************
Private Function GetIP(strHost)

    On Error Resume Next

    ' Ping Host and Extract IP Address
    Dim objShell: Set objShell = WScript.CreateObject("WScript.Shell")
    Dim objPing: Set objPing = objShell.Exec("%comspec% /c For /f " & chr(34) & "tokens=2 delims=[]" & chr(34) & " %A In ('Ping -a -n 1 " & strHost & "') Do %A")
    Dim strPingResults: strPingResults = objPing.StdOut.ReadAll

    ' Search For an IP Address from Results
    Dim RegEx: Set RegEx = New RegExp
    RegEx.IgnoreCase = True
    RegEx.Global = True
    RegEx.Pattern = "[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}"
    If Regex.Test(strPingResults) = True then
        Dim Matches: Set Matches = RegEx.Execute(strPingResults)
        Dim strMatch
        For Each strMatch in Matches
            ' Return Result
            GetIP = strMatch
        Next
    Else
        GetIP = "!~ERROR~!"
    End If

    ' Kill Objects
    Set objShell = Nothing

    On Error Goto 0

End Function

' **************************************************************************************************
' Function GetMAC - Returns a MAC Address From Passing an IP Into the Function
' ********************************************************************
Private Function GetMAC(strIP)

        On Error Resume Next

        ' Check ARP Table and Results
        Dim objShell: Set objShell = WScript.CreateObject("WScript.Shell")
        Dim objArp: Set objArp = objShell.Exec("arp -a " & strIP)
        Dim strArpResult: strArpResult = objArp.StdOut.ReadAll

        ' Search For a MAC Address from Results
       Dim RegEx: Set RegEx = New RegExp
        RegEx.IgnoreCase = True
        RegEx.Global = True
        RegEx.Pattern = "[0-9A-F]{2}-[0-9A-F]{2}-[0-9A-F]{2}-[0-9A-F]{2}-[0-9A-F]{2}-[0-9A-F]{2}"
        If Regex.Test(strArpResult) = True then
                Dim Matches: Set Matches = RegEx.Execute(strArpResult)
                Dim strMatch
                For Each strMatch in Matches
                    GetMAC = strMatch
                Next
        Else
            GetMAC = "!~ERROR~!"
        End if

        ' Kill Objects
        Set objShell = Nothing

        On Error Goto 0

End Function

 

Jan 01

GenerateIPv4Addresses.vbs

'=========================================================================
' GenerateIPv4Addresses.vbs
' VERSION: 1.0
' AUTHOR: Brian Steinmeyer
' EMAIL: sigkill@sigkillit.com
' WEB: http://sigkillit.com
' DATE: 1/1/2011
' COMMENTS: Input the Start IPv4 IP Address and the End IPv4 IP Address to
' Generate All IP Addresses In a Log File for the Given Range.
' EXAMPLE: Input the Starting IPv4 Address:    192.168.1.1
'          Input the Ending IPv4 Address: 192.168.1.255
'=========================================================================
Option Explicit

' Generate IPv4 Addresses
Dim ipStart: ipStart = InputBox("Input the Starting IPv4 Address")
Dim ipEnd: ipEnd = InputBox("Input the Ending IPv4 Address")
Call Logger("GenerateIPv4Addresses.txt", GenerateIPv4Addresses(ipStart, ipEnd), True)
Wscript.Echo "Finished"

Private Function GenerateIPv4Addresses(ipStart, ipEnd)

On Error Resume Next

' Validate IPv4 Address
Dim strResult: strResult = ""
If ValidateIPv4(ipStart) = False OR ValidateIPv4(ipEnd) = False Then
strResult = "Invalid IP Range: " & ipStart & " - " & ipEnd
Else
' Generate IP Range
Dim ipOctetStart: ipOctetStart = Split(ipStart,".")
Dim ipOctetEnd: ipOctetEnd = Split(ipEnd,".")
Dim i, oct1,oct2,oct3,oct4, blnInitial: blnInitial = True
For oct1 = ipOctetStart(0) to ipOctetEnd(0)
For oct2 = ipOctetStart(1) to ipOctetEnd(1)
For oct3 = ipOctetStart(2) to ipOctetEnd(2)
If blnInitial = True Then
blnInitial = False
If StrComp(oct1, ipOctetEnd(0)) = 0 AND StrComp(oct2, ipOctetEnd(1)) = 0 AND StrComp(oct3, ipOctetEnd(2)) = 0 Then
' Initial Loop on Octet4 is the Final Loop
For oct4 = ipOctetStart(3) to ipOctetEnd(3)
strResult = strResult & oct1 & "." & oct2 & "." & oct3 & "." & oct4 & vbCrLf
Next
Else
' Initial Loop on Octet4 is Not the Final Loop
For oct4 = ipOctetStart(3) to 255
strResult = strResult & oct1 & "." & oct2 & "." & oct3 & "." & oct4 & vbCrLf
Next
End If
Else
If StrComp(oct1, ipOctetEnd(0)) = 0 AND StrComp(oct2, ipOctetEnd(1)) = 0 AND StrComp(oct3, ipOctetEnd(2)) = 0 Then
' Non-Initial Loop is the Final Loop
For oct4 = 0 to ipOctetEnd(3)
strResult = strResult & oct1 & "." & oct2 & "." & oct3 & "." & oct4 & vbCrLf
Next
Else
' Non-Initial Loop is Not the Final Loop
For oct4 = 0 to 255
strResult = strResult & oct1 & "." & oct2 & "." & oct3 & "." & oct4 & vbCrLf
Next
End If
End If
Next
Next
Next
End If

' Return Results
GenerateIPv4Addresses = strResult

On Error Goto 0

End Function

Private Function ValidateIPv4(ip)

On Error Resume Next

' Validate IPv4 Address
Dim blnValid: blnValid = True
Dim arrIP: arrIP = Split(ip,".")
If UBound(arrIP) = 3 Then
Dim i
For i = LBound(arrIP) to UBound(arrIP)
If IsNumeric(arrIP(i)) = True Then
If arrIP(i) > 255 Then
blnValid = False
End If
Else
blnValid = False
End If
Next
Else
blnValid = False
End If

' Check For Errors
If Err.Number <> 0 Then
blnValid = False
Err.Clear
End If

' Return Result
If blnValid = True Then
ValidateIPv4 = True
Else
ValidateIPv4 = False
End If

On Error Goto 0

End Function

Private Sub Logger(fileName, logMessage, blnNewLog)

On Error Resume Next

Const ForReading = 1, ForWriting = 2, ForAppending = 8
Dim objFSO: Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim scriptPath: scriptPath = Left(WScript.ScriptFullName,InstrRev(WScript.ScriptFullName,"\"))
Dim logName
If InStr(1,fileName,"\",1) > 0 Then
logName = fileName
If objFSO.DriveExists(objFSO.GetDriveName(logName)) Then
If StrComp(objFSO.GetExtensionName(logName), "", 1) = 0 Then
If Not objFSO.FolderExists(logName) Then
If objFSO.FolderExists(objFSO.GetParentFolderName(logName)) Then
objFSO.CreateFolder logName 'Create Folder In Current Path
Exit Sub
Else
Call Logger(objFSO.GetParentFolderName(logName), logMessage, blnNewLog) 'Recurse Creating Parent Folder
Call Logger(logName, logMessage, blnNewLog) 'Recurse Creating Current Folder
Exit Sub
End If
End If
Else
If Not objFSO.FileExists(logName) Then
If Not objFSO.FolderExists(objFSO.GetParentFolderName(logName)) Then
Call Logger(objFSO.GetParentFolderName(logName), logMessage, blnNewLog)  'Recurse Creating Parent Folder
Call Logger(logName, logMessage, blnNewLog)  'Recurse Creating Current Folder
End If
End If
End If
End If
Else
logName = scriptPath & fileName
End If
Dim logFile
If blnNewLog = True Then
Set logFile = objFSO.CreateTextFile(logName, True)
Else
If objFSO.FileExists(logName) Then
Set logFile = objFSO.OpenTextFile(logName, ForAppending, True)
Else
Set logFile = objFSO.CreateTextFile(logName, True)
End If
End If
logFile.WriteLine logMessage
logFile.Close
Set objFSO = Nothing

On Error Goto 0

End Sub