Sign in to follow this  
Followers 0
Jkeith247

Get subnet associated with IP

3 posts in this topic

#1 ·  Posted (edited)

Hello all,

I think I may be having an "off" day today as my mind seems weaker than usual.... I have been searching for some time now, trying to find a way to pull the subnet associated with an IP address and store it in a variable. Also, I have more than one network adapter on my computer thus I have more than one IP address...

Basically, what I am trying to do is perform the calculation to determine the proper broadcast address for a network and I need the IP and Subnet to do this... The IP is the easy part, the subnet not so easy... Does anyone have any suggestions? I dabled around a bit with Win32_NetworkAdapterConfiguration but this seemed to be rather troublesome just to get the subnet address....

Any help is greatly appreciated. Thanks!!

Edited by Jkeith247

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Scriptomatic takes the troublesome out of it, imho.

returns all IPs and their corresponding subnets. Commented lines, if uncommented, will ask for an IP and retrieve only the subnet for that IP (if none found then returned all). Seemed less efficient than just looking at the entire list, but you may find a use.

;~ $IP = inputbox ("Which IP?" , "Which IP do you want the subnet for?")

$wbemFlagReturnImmediately = 0x10
$wbemFlagForwardOnly = 0x20
$colItems = ""
$strComputer = "localhost"

$Output=""
$FOutput=""
$objWMIService = ObjGet("winmgmts:" & $strComputer & "rootCIMV2")
$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration", "WQL", _
                                          $wbemFlagReturnImmediately + $wbemFlagForwardOnly)

If IsObj($colItems) then
   For $objItem In $colItems

      $strIPAddress = $objItem.IPAddress(0)
      If $strIPAddress <> "" Then $FOutput &= $strIPAddress & @CRLF

      $strIPSubnet = $objItem.IPSubnet(0)
      If $strIPSubnet <> "" Then $FOutput &= $strIPSubnet & @CRLF & @CRLF

;~  If $strIPAddress = $IP Then $Output &= $strIPSubnet
   Next
Else
   Msgbox(0,"WMI Output","No WMI Objects Found for class: " & "Win32_NetworkAdapterConfiguration" )
Endif


;~ If $output <> "" Then
;~ msgbox (0, '' , $output)
;~ Else
    msgbox (0, '' , $Foutput)
;~   Endif

Func WMIDateStringToDate($dtmDate)

    Return (StringMid($dtmDate, 5, 2) & "/" & _
    StringMid($dtmDate, 7, 2) & "/" & StringLeft($dtmDate, 4) _
    & " " & StringMid($dtmDate, 9, 2) & ":" & StringMid($dtmDate, 11, 2) & ":" & StringMid($dtmDate,13, 2))
EndFunc
Edited by boththose
1 person likes this

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
Share on other sites

This is awesome. Thank you very much. This is exactly what I was looking for. Scriptomatic huh? I am gonna have to take a look at that.

Thanks again!

Share this post


Link to post
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
Sign in to follow this  
Followers 0