At times it can be very convenient to be able to work with IP Addresses and subnets in VbScript. This collection of functions handles subnet maths in VbScript.

A PowerShell version of these functions can be found here.

Convert an IP to binary

This function performs a bitwise comparison (AND) with each octet in the original IP to determine if each power of two is present starting with the highest, 128 returning the binary version of any IP address.

Convert a binary IP to a decimal IP

It is important to be able to convert back into the familiar form of the IP address, this calculates the decimal form of an IP address based on the binary.

Convert a subnet mask to a mask length

Occasionally it is desirable to calculate the subnet mask bit length. This can be done using the following.

Convert a mask length to a subnet mask

Calculate the subnet network address

The functions above can be used with along with a bitwise AND operation against an IP address and subnet mask to calculate the network address.

Note that this function includes calls to both ConvertIPToBinary and ConvertBinIPToDecimal.

Calculate the subnet broadcast address

To calculate the broadcast address requires a modification of the CalcNetworkAddress function above. This time, it sets anything that is not covered by the mask to 1.

Note that this function includes calls to both ConvertIPToBinary and ConvertBinIPToDecimal.

12 Comments

  1. This is all very impressive. I am able to get most of these to work for what I need, which is fantastic. However, I cannot for the life of me get the ‘Calculate the subnet network address’ function(s) to work.

    It just returns a string like so;

    00001010.00011110.00110011.00111110 11111111.11111111.11111111.00000000 0 0 00001010.00011110.00110011.00000000

    I am trying to get the network address (e.g. 192.168.0/24) from the IP address and Subnet mask;

    strIP = “192.168.0.100”
    strMask = “255.255.255.0”

    CalcNetworkAddress strIP, strMask

    I am sure I am missing something.

    TIA !
    Jon

    Reply

  2. Thanks a lot for these scripts. I them in one of my script.

    I have written two extra functions (based on yours), which returns the first and last IP address of a subnet. (usefull for some tools).

    I place my contribution in public domain.
    BTW, Could you clarify the license of your script (typically Public domain, BSD license, or GPL license).

    Reply

  3. Hey Franklin,

    Thanks for the contribution ๐Ÿ™‚

    All code is intended to be public domain, I’ll post an explicit statement to that effect somewhere.

    Chris

    Reply

  4. Hi Jon,

    Sorry for the delay in getting back to you, I’ve been on holiday so a bit out of touch.

    The functions return the correct value for me, did you also include the ConvertIPToBinary and ConvertBinIPToDecimal?

    Normally you would use the function something like this:

    strNetAddr = CalcNetworkAddress(strIP, strMask)
    Or
    WScript.Echo CalcNetworkAddress(strIP, strMask)

    Chris

    Reply

  5. Thanks for these functions. I used them, and the information on some of your other pages to write the following functions:

    Reply

  6. Realy great collection of scripts.
    Right the scripts I was looking for.
    Thanks.

    Reply

  7. Great work. functions work perfectly and have saved me a heap of work.

    Awesome ๐Ÿ™‚

    Reply

  8. Beautiful functions!
    Thumbs up!

    ๐Ÿ™‚

    Reply

  9. If someone is looking for a very fast code to do some IP- and subnet calculations via vbscript, then I would recommend my post here:

    http://www.codeproject.com/Messages/3779227/Here-is-a-very-fast-code-for-calculating-IP-and-su.aspx

    Enjoy!

    Reply

    • Good work. Bit-operations are certainly the way to go, I did the same for the PowerShell version of these functions.

      Chris

      Reply

  10. Very nice. Just what I needed. Thanks so much for sharing!!!

    Reply

  11. The snippets on this post are a God-send, thanks a lot.

    Bellow are two quick wrapper functions I added for ease of use (see below for a sample use):

    [code language=”vb”]
    function IpToNumber(sIP)
    ‘Converts an IP-string into a large positive number.
    dim octet
    for each octet in split(sIP, ".")
    IpToNumber = IpToNumber * 256 + CByte(octet)
    next
    end function

    function isIpInRange(sIP, sIpFrom, sIpTo)
    dim lTarget

    isIpInRange = false

    lTarget = IpToNumber(sIP)
    if lTarget < IpToNumber(sIpFrom) then exit function
    if lTarget > IpToNumber(sIpTo) then exit function

    isIpInRange = true
    end function

    For example, say you need to test if 192.168.2.100 is contained in 192.168.2.0/22:
    First you need to translate 192.168.2.0/22 into the upper and lower bounds, then see if the target IP is in range:

    [code language=”vb”]
    sTargetIP = "192.168.2.100"
    sMaskLength = "22"

    bFound = isIpInRange(sTargetIP, CalcRangeStart(sRangeIP, MaskLengthToIP(sMaskLength)), CalcRangeEnd(sRangeIP, MaskLengthToIP(sMaskLength))))

    Reply

  12. Pingback: IP Blocklist to Cisco ASA access-list VBscript | thejimmahknows

Leave a Reply

Your email address will not be published. Required fields are marked *