Jump to content

Subnet and Broadcast Address Calculator


archrival
 Share

Recommended Posts

I've updated my subnet functions, they've worked in all situations in which I've tried them, but feel free to point out any errors you see. Thank you. The first function will return an array conatining:

Given this information (ex. IP: 192.168.1.142, NetMask: 255.255.255.0)

1. The subnet address (ex. 192.168.1.0)

2. The broadcast address (ex. 192.168.1.255)

3. The wildcard address (ex. 0.0.0.255)

4. The IP range of the given subnet, IP (ex. 192.168.1.1-192.168.1.254)

5. The number of hosts on the subnet (ex. 254)

6. The maskslash (ex. 192.168.1.0/24)

The second function determines whether the given IP address falls in the same subnet as the given subnet and broadcast addresses. It's main use would be combined with information received from the first function.

CODE
Func _Subnet($sIp, $sNetmask)

Dim $netmaskbinary

Dim $subnetaddarray[5]

Dim $invmaskarray[5]

Dim $broadcastaddarray[5]

Dim $sSubnetinfo[7]

Dim $subnetadd

Dim $invmask

Dim $broadcastadd

; Reads IP and Netmask to an array

$iparray = StringSplit($sIp, ".")

$netmaskarray = StringSplit($sNetmask, ".")

; Validates IP address

For $i = 1 To 4

If Number($iparray[$i]) < 0 Or Number($iparray[$i]) > 255 Then

SetError(1)

Return (-1)

EndIf

Next

; Converts netmask into a decimal

$netmaskdec = ($netmaskarray[1] * 16777216) + ($netmaskarray[2] * 65536) + ($netmaskarray[3] * 256) + $netmaskarray[4]

; Converts decimal netmask into binary (ex. 11111111111111111100000000000000)

While $netmaskdec <> 0

$binmod = Mod($netmaskdec, 2)

$netmaskbinary = $binmod & $netmaskbinary

$netmaskdec = Int($netmaskdec / 2)

WEnd

; Determines the "slash" value of the netmask

$maskslash = StringInStr($netmaskbinary, "0", 1) - 1

; Validates the "slash" value and netmask value

If StringInStr(StringRight($netmaskbinary, 32 - $maskslash), "1") Then

If $netmaskarray[4] = "255" Then

$maskslash = 32

Else

SetError(1)

Return (-1)

EndIf

EndIf

; Creates arrays conatining subnet address, wilcard, and broadcast addresses

For $i = 1 To $iparray[0]

$subnetaddarray[$i] = BitAND($iparray[$i], $netmaskarray[$i])

$invmaskarray[$i] = BitNOT($netmaskarray[$i] - 256)

$broadcastaddarray[$i] = BitOR($subnetaddarray[$i], $invmaskarray[$i])

Next

; Creates strings conatining subnet address, wilcard, and broadcast addresses

$subnetadd = $subnetaddarray[1] & "." & $subnetaddarray[2] & "." & $subnetaddarray[3] & "." &$subnetaddarray[4]

$invmask = $invmaskarray[1] & "." & $invmaskarray[2] & "." & $invmaskarray[3] & "." & $invmaskarray[4]

$broadcastadd = $broadcastaddarray[1] & "." & $broadcastaddarray[2] & "." & $broadcastaddarray[3] & "." & $broadcastaddarray[4]

If $maskslash = 32 Then

$iprange = $iparray[1] & "." & $iparray[2] & "." & $iparray[3] & "." & $iparray[4]

$hosts = 1

Else

; Determines the IP range for this subnet

$iprange = $subnetaddarray[1] & "." & $subnetaddarray[2] & "." & $subnetaddarray[3] & "." & $subnetaddarray[4] + 1 & _

"-" & $broadcastaddarray[1] & "." & $broadcastaddarray[2] & "." & $broadcastaddarray[3] & "." & $broadcastaddarray[4] - 1

; Calculates number of available hosts on this subnet

$hosts = ($invmaskarray[4] + 1) * ($invmaskarray[3] + 1) * ($invmaskarray[2] + 1) * ($invmaskarray[1] + 1) - 2

EndIf

$sSubnetinfo[1] = $subnetadd

$sSubnetinfo[2] = $broadcastadd

$sSubnetinfo[3] = $invmask

$sSubnetinfo[4] = $iprange

$sSubnetinfo[5] = $hosts

$sSubnetinfo[6] = $maskslash

Return ($sSubnetinfo)

EndFunc

Func _SameSub($sIp, $sSubadd, $sBroadadd)

Dim $iparray[5]

Dim $subaddarray[5]

Dim $broadaddarray[5]

$iparray = StringSplit($sIp, ".")

$subaddarray = StringSplit($sSubadd, ".")

$broadaddarray = StringSplit($sBroadadd, ".")

For $i = 1 To 4

If Number($iparray[$i]) < 0 Or Number($iparray[$i]) > 255 Then

SetError(1)

Return (-1)

EndIf

If Number($subaddarray[$i]) < 0 Or Number($subaddarray[$i]) > 255 Then

SetError(1)

Return (-2)

EndIf

If Number($broadaddarray[$i]) < 0 Or Number($broadaddarray[$i]) > 255 Then

SetError(1)

Return (-3)

EndIf

Next

$ipint = ($iparray[1] * 16777216) + ($iparray[2] * 65536) + ($iparray[3] * 256) + $iparray[4]

$subaddint = ($subaddarray[1] * 16777216) + ($subaddarray[2] * 65536) + ($subaddarray[3] * 256) + $subaddarray[4]

$broadaddint = ($broadaddarray[1] * 16777216) + ($broadaddarray[2] * 65536) + ($broadaddarray[3] * 256) + $broadaddarray[4]

If $ipint > $subaddint And $ipint < $broadaddint Then

Return (1)

Else

Return (0)

EndIf

EndFunc

Edited by archrival
Link to comment
Share on other sites

  • 3 weeks later...

I used the top one and found a problem. Not every ipconfig.txt will show Subnet Mask on line 17. This worked on XP but on a 2000 laptop I had to change it to line 14. I haven't tried the bottom on. How does it work? Do I run the function?

Link to comment
Share on other sites

Fixed the original. Added a while statement and 2 new vars. $linecount and $submasktext.

While statement runs until $submasknet = "SubnetMask." linecount keeps going up on to represent the next line that $parse reads. If it reaches the end of the file then it exits the loop. It will error out at that point I am sure since array won't work.

#comments-start
Routine to extract the IP address and other useful information from a PC. The PC must be able to run ipconfig in dos (so no Win 95). Also There must be a C:\
Duh!

#comments-end
 
$ip = @IPAddress1
RunWait(@ComSpec & " /c ipconfig > " & "c:\ipconfig.txt", "", @SW_HIDE)
$parse = FileReadLine("c:\ipconfig.txt", 1)
$submasktext = StringLeft(StringStripWS($parse, 8), 11)
$netmask = StringStripWS(StringTrimLeft($parse, StringInStr($parse, "Subnet Mask") + 35), 8)
$linecount = 1
 
;Find Subnet Mask statement in txt file if not on line 1

While $submasktext <> "SubnetMask."
 $parse = FileReadLine("C:\ipconfig.txt", $linecount)
 If @error = -1 Then ExitLoop
 $submasktext = StringLeft(StringStripWS($parse, 8), 11)
 $netmask = StringStripWS(StringTrimLeft($parse, StringInStr($parse, "Subnet Mask") + 35), 8)
 $linecount = $linecount + 1
Wend
 
$iparray = StringSplit($ip, ".")
$netmaskarray = StringSplit($netmask, ".")
 
Dim $subnetaddarray[5]
Dim $invmaskarray[5]
Dim $broadcastaddarray[5]
 
For $i = 1 To $iparray[0]
   $subnetaddarray[$i] = BitAND($iparray[$i], $netmaskarray[$i])
   $invmaskarray[$i] = BitNOT($netmaskarray[$i] - 256)
   $broadcastaddarray[$i] = BitOR($subnetaddarray[$i], $invmaskarray[$i])
Next
 
$broadcastadd = $broadcastaddarray[1] & "." & $broadcastaddarray[2] & "." & $broadcastaddarray[3] & "." & $broadcastaddarray[4]
 
$subnetadd = $subnetaddarray[1] & "." & $subnetaddarray[2] & "." & $subnetaddarray[3] & "." & $subnetaddarray[4]
 
$invmask = $invmaskarray[1] & "." & $invmaskarray[2] & "." & $invmaskarray[3] & "." & $invmaskarray[4]
 
$iprange = $subnetaddarray[1] & "." & $subnetaddarray[2] & "." & $subnetaddarray[3] & "." & $subnetaddarray[4] + 1 & "-" & $broadcastaddarray[1] & "." & $broadcastaddarray[2] & "." & $broadcastaddarray[3] & "." & $broadcastaddarray[4] - 1
 
$hosts = ($invmaskarray[4] + 1) * ($invmaskarray[3] + 1) * ($invmaskarray[2] + 1) - 2
 
MsgBox(0, "IP Info", "IP Address: " & $ip & @CRLF & "Subnet Mask: " & $netmask & @CRLF & "Wildcard: " & $invmask & @CRLF & "Broadcast Address: " & $broadcastadd & @CRLF & "Subnet Address: " & $subnetadd & @CRLF & "IP Range: " & $iprange & @CRLF & "Hosts: " & $hosts & @CRLF & "This PC's Name: " & @ComputerName)
 
If FileExists("c:\ipconfig.txt") Then
   FileDelete("c:\ipconfig.txt")
EndIf
Edited by tazdev
Link to comment
Share on other sites

  • 8 months later...

Oh, I use that all the time, I just like to write my own programs if they work just as well and take less resources...

I'm actually the lead for a Network Design Team for BPA...Unfortunately this year the national competition is 20 miles away ;)

Writing AutoIt scripts since

_DateAdd("d", -2, _NowCalcDate())
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...