GenerateIPv4Addresses.vbs

'=========================================================================
' GenerateIPv4Addresses.vbs
' VERSION: 1.0
' AUTHOR: Brian Steinmeyer
' EMAIL: [email protected]
' WEB: https://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