(e-mail address removed) (Rob Willis) wrote in
Wayne,
Thanks - that's great! Could you please post the script to the list -
I'd really like to try this out soon. One question - does your script
deal with "catchall" type subnet definitions? For example, we have
subnet definitions for entire class B networks for a site, with other
subnets within that class B defined as a class C and a different site
- this prevents us from having to define every class C network in the
class B since most of them are at the same site.
Thanks again,
Rob
Rob,
The script correctly handles catchall subnets by finding the smallest
subnet object that contains the specified address. It also has 'Default-
First-Site-Name' hardcoded so it may display that site name even if it
doesn't exist (has been renamed).
This is going to wrap horribly so take care reconstructing it...
Wayne
IP2Site.vbs:
'------------------------------------------------------------------------
------'
' Displays the AD Site name for the supplied dotted decimal IP address.
'
'------------------------------------------------------------------------
------'
' IP2Site.vbs witten April 4, 2003 by Wayne Tilton
'------------------------------------------------------------------------
------'
Option Explicit
WScript.Echo SiteName(WScript.Arguments.Item(0))
Function SiteName(IPAddr)
Dim ConfigNameContext
Dim DecIPAddr, MaskBits, NumAddrs, LoIPAddr, HiIPAddr, SiteAddrs
Dim SubnetContainer, LDAPQry, Subnet
Dim AdConn, AdComm, AdRS
SiteName = "Default-First-Site-Name" ' Default site name if
no matches
DecIPAddr = Dot2Dec(IPAddr) ' Convert supplied
address to decimal
ConfigNameContext = GetObject("LDAP://RootDSE").Get
("configurationNamingContext")
SubnetContainer = "CN=Subnets,CN=Sites," & ConfigNameContext
LDAPQry = "<LDAP://" & SubnetContainer & ">;
(objectCategory=subnet);cn,siteObject"
Set AdConn = CreateObject("ADODB.Connection") ' Get an ADO connection
object
AdConn.Provider = "ADsDSOObject" ' Set provider name
AdConn.Open "Active Directory Provider" ' open connection
Set AdComm = CreateObject("ADODB.Command") ' Get an ADO command
object
Set AdComm.ActiveConnection = AdConn ' Tell command object
about connection
AdComm.Properties("SearchScope") = 2 ' we want to search
everything
AdComm.Properties("Page Size") = 500 ' and we want our
records in lots of 500
AdComm.CommandText = LDAPQry ' Set the ADO
CommandText
Set AdRS = AdComm.Execute ' and run the query.
SiteAddrs = (2^31)-1 ' Really big range
AdRS.MoveFirst ' Go to 1st record in
the set
While Not AdRS.EOF ' Read 'em until they're
gone
Subnet = AdRs.Fields("cn") ' Get subnet name
MaskBits = Split(Subnet,"/") ' Split IP addr from
subnet mask
NumAddrs = (2 ^ (32 - MaskBits(1))) - 1 ' Calc # addresses based
on subnet mask
LoIPAddr = Dot2Dec(MaskBits(0)) ' Get low end of IP
range in decimal
HiIPAddr = LoIPAddr + NumAddrs ' Calc high end of range
by adding # add
' Check to see if IP is within this subnet range and if so that
it's smallest subnet with that addr
If DecIPAddr => LoIPAddr And DecIPAddr <= HiIPAddr And NumAddrs <=
SiteAddrs then
SiteName = AdRs.Fields("siteObject")
SiteName = GetObject("LDAP://" & AdRs.Fields("siteObject")).Get
("name")
SiteAddrs = NumAddrs ' Remember # addrs in
this subnet
End If
AdRS.MoveNext ' Go to next record or
EOF
Wend
Set AdRs = Nothing
Set AdComm = Nothing
Set AdConn = Nothing
End Function
' Returns the decimal value of a dotted decimal IP address
Function Dot2Dec(IPAddress)
Dim Octets
Octets = Split(IPAddress,".")
Dot2Dec = (Octets(0)*(2^24)) + (Octets(1)*(2^16)) + (Octets(2)*(2^8))
+ Octets(3)
End Function