Submitted By: Pasquale Attanasio

Sample function that takes an IP address and returns the next address in the sequence.

Visual Basic
Edit|Remove
'
'  IncIP.vbs - April 17, 2006 - Pasquale Attanasio
'
'  The IncIp function takes an IP address and returns the next address in the sequence.
'
'  It wraps at 255.255.255.255, returning 1.1.1.1
'
'  Refer to the test cases at the end of the script.

Option Explicit

Function IncIp(IPAddr)

Dim Oct				' Array of Octets
Dim i
Dim cf				' Carry Flag


  Oct = Split(IpAddr,".",-1,vbTextCompare)
  cf = True
  For i = UBound(Oct) to 0 Step -1
    If cf then
      If Oct(i) = 255 then
        Oct(i) = 1
      Else
        Oct(i) = Oct(i) + 1
        cf = False
      End If    
    End If 
  Next
  IncIP = Oct(0) & "." & Oct(1) & "." & Oct(2) & "." & Oct(3)

End Function  


' Test Cases

WScript.Echo IncIp("172.16.100.10")
WScript.Echo IncIp("172.16.100.254")
WScript.Echo IncIp("172.16.100.255")
WScript.Echo IncIp("172.16.254.255")
WScript.Echo IncIp("172.16.255.255")
WScript.Echo IncIp("172.255.255.255")
WScript.Echo IncIp("255.255.255.255")

MsgBox "Display every possible IP address..."

' (This runs for a while...)

Dim NewIP

NewIp = "1.1.1.1"
Do
  NewIp = IncIp(NewIp)
  WScript.Echo NewIp
Loop Until (NewIp = "1.1.1.1")