home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 May / W2KPRK.iso / netmgmt.cab / subnet_op.vbs < prev    next >
Text File  |  1999-11-18  |  17KB  |  725 lines

  1. '----------------------------------------------------------------------
  2. '
  3. ' Copyright (c) Microsoft Corporation 1998-1999
  4. ' All Rights Reserved
  5. '
  6. ' Abstract:
  7. '
  8. ' subnet_op.vbs - subnet operation script for Windows 2000 DS
  9. '
  10. ' Usage:
  11. '
  12. ' subnet_op [-adl?] [-n subnet-name] [-s site-name] [-p location]
  13. '           [-i ip address] [-m subnet mask]
  14. '
  15. '----------------------------------------------------------------------
  16.  
  17. option explicit
  18.  
  19. '
  20. ' Debugging trace flags.
  21. '
  22. const kDebugTrace = 1
  23. const kDebugError = 2
  24.  
  25. dim gDebugFlag
  26.  
  27. '
  28. ' To enable debug output trace message
  29. ' Change the following variable to True.
  30. '
  31. gDebugFlag = False
  32.  
  33. '
  34. ' Messages to be displayed if the scripting host is not cscript
  35. '                            
  36. const kMessage1 = "Please run this script using CScript."  
  37. const kMessage2 = "This can be achieved by"
  38. const kMessage3 = "1. Using ""CScript script.vbs arguments"" or" 
  39. const kMessage4 = "2. Changing the default Windows Scripting Host to CScript"
  40. const kMessage5 = "   using ""CScript //H:CScript //S"" and running the script "
  41. const kMessage6 = "   ""script.vbs arguments""."
  42.  
  43. '
  44. ' Operation action values.
  45. '
  46. const kActionAdd        = 0
  47. const kActionDelete     = 1
  48. const kActionList       = 2
  49. const kActionCalculate  = 3
  50. const kActionUnknown    = 4
  51.  
  52. main
  53.  
  54. '
  55. ' Main execution start here
  56. '
  57. sub main
  58.  
  59.     on error resume next
  60.  
  61.     dim strSubnet
  62.     dim strSite
  63.     dim strLocation
  64.     dim strIpAddress
  65.     dim strSubnetMask
  66.     dim iAction
  67.     dim oSubnet
  68.     dim iRetval
  69.     dim oArgs
  70.     
  71.     '
  72.     ' Abort if the host is not cscript
  73.     '
  74.     if not IsHostCscript() then
  75.    
  76.         call wscript.echo(kMessage1 & vbCRLF & kMessage2 & vbCRLF & _
  77.                           kMessage3 & vbCRLF & kMessage4 & vbCRLF & _
  78.                           kMessage5 & vbCRLF & kMessage6 & vbCRLF)
  79.         
  80.         wscript.quit
  81.    
  82.     end if
  83.  
  84.     iAction = kActionUnknown
  85.  
  86.     iRetval = ParseCommandLine( iAction, strSubnet, strSite, strLocation, strIpAddress, strSubnetMask )
  87.  
  88.     if iRetval = 0 then
  89.  
  90.         select case iAction
  91.  
  92.             case kActionAdd
  93.                 iRetval = CreateSubnetObject( oSubnet, strSubnet, strSite, strLocation )
  94.  
  95.             case kActionDelete
  96.                 if strSubnet = "all" then
  97.                     iRetval = DeleteAllSubnetObjects( )
  98.                 else
  99.                     iRetval = DeleteSubnetObject( strSubnet )
  100.                 end if
  101.  
  102.             case kActionList
  103.                 iRetval = ListSubnetObjects( )
  104.  
  105.             case kActionCalculate
  106.                 iRetval = CalculateSubnetObject( strIpAddress, strSubnetMask )
  107.  
  108.             case else
  109.                 Usage( True )
  110.                 exit sub
  111.  
  112.         end select
  113.  
  114.     end if
  115.  
  116. end sub
  117.  
  118.  
  119. '
  120. ' Calculate the subnet object name an IP address and a subnet mask
  121. '
  122. function CalculateSubnetObject( ByRef strIpAddress, ByRef strSubnetMask )
  123.  
  124.     on error resume next
  125.  
  126.     DebugPrint kDebugTrace, "In the CalculateSubnetObject"
  127.  
  128.     Dim aIpAddressOctetArray(4)
  129.     Dim aSubnetMaskOctetArray(4)
  130.     Dim strSubnetObjectName
  131.  
  132.     if CreateValueFromOctetString( strIpAddress, aIpAddressOctetArray ) = False then
  133.  
  134.         wscript.echo "Invalid IP Address, must be of the form ddd.ddd.ddd.ddd, e.g. 157.41.50.2"
  135.  
  136.         CalculateSubnetObject = False
  137.  
  138.         exit function
  139.  
  140.     end if
  141.  
  142.     if CreateValueFromOctetString( strSubnetMask, aSubnetMaskOctetArray ) = False then
  143.  
  144.         wscript.echo "Invalid Subnet mask, must be of the form ddd.ddd.ddd.ddd, e.g. 255.255.252.0"
  145.  
  146.         CalculateSubnetObject = False
  147.  
  148.         exit function
  149.  
  150.     end if
  151.  
  152.     DebugPrint kDebugTrace, "Ip Octet Value " & aIpAddressOctetArray(0) & "." & aIpAddressOctetArray(1) & "." & aIpAddressOctetArray(2) & "." & aIpAddressOctetArray(3)
  153.     DebugPrint kDebugTrace, "Mask Octet Value " & aSubnetMaskOctetArray(0) & "." & aSubnetMaskOctetArray(1) & "." & aSubnetMaskOctetArray(2) & "." & aSubnetMaskOctetArray(3)
  154.  
  155.     if ConvertToSubnetName( aIpAddressOctetArray, aSubnetMaskOctetArray, strSubnetObjectName ) = True then
  156.  
  157.         wscript.echo "Subnet Object Name:", strSubnetObjectName
  158.  
  159.         CalculateSubnetObject = True
  160.  
  161.     else
  162.  
  163.         wscript.echo "Unable to convert IP address and subnet mask to subnet object name"
  164.  
  165.         CalculateSubnetObject = False
  166.  
  167.     end if
  168.  
  169. end function
  170.  
  171. '
  172. ' Convert the given ip address and subnet mask to a DS object name.
  173. ' The algorithm is to take the ip address and it with the subnet mask
  174. ' this is the subnet then tack on a slash and place the number of bits
  175. ' in the subnet mask at the end.  For example ip address of 157.59.16.2
  176. ' subnet mask 255.255.252.0 results in a subnet object name of 157.59.0.0/22
  177. '
  178. function ConvertToSubnetName( ByRef aIpOctetArray, ByRef aSubnetOctetArray, ByRef strSubnet )
  179.  
  180.     on error resume next
  181.  
  182.     DebugPrint kDebugTrace, "In the ConvertToSubnetObject"
  183.  
  184.     Dim iVal0
  185.     Dim iVal1
  186.     Dim iVal2
  187.     Dim iVal3
  188.     Dim iBits
  189.     Dim i
  190.     Dim j
  191.  
  192.     iVal0 = aIpOctetArray(0) and aSubnetOctetArray(0)
  193.     iVal1 = aIpOctetArray(1) and aSubnetOctetArray(1)
  194.     iVal2 = aIpOctetArray(2) and aSubnetOctetArray(2)
  195.     iVal3 = aIpOctetArray(3) and aSubnetOctetArray(3)
  196.  
  197.     for i = 0 to 3
  198.  
  199.         for j = 0 to 7
  200.  
  201.             if (aSubnetOctetArray(i) and (2 ^ j)) <> 0 then
  202.  
  203.                 iBits = iBits + 1
  204.  
  205.             end if
  206.  
  207.         next
  208.  
  209.     next
  210.  
  211.     strSubnet = iVal0 & "." & iVal1 & "." & iVal2 & "." & iVal3 & "/" & iBits
  212.  
  213.     ConvertToSubnetName = True
  214.  
  215. end function
  216.  
  217. '
  218. ' Converts an octet string to an array of octet values. For example
  219. ' this function when given string 157.59.161.2 will return an array with
  220. ' the followin values. A(0) = 157, A(1) = 59, A(2) = 161, A(3) = 2
  221. '
  222. function CreateValueFromOctetString( ByRef strOctet, ByRef aOctetArray )
  223.  
  224.     on error resume next
  225.  
  226.     DebugPrint kDebugTrace, "In the CreateValueFromOctetString"
  227.  
  228.     Dim i
  229.     Dim iDotCount
  230.     Dim cValue
  231.     Dim iValue
  232.  
  233.     for i = 0 to 32
  234.  
  235.         cValue = Mid( strOctet, i, 1 )
  236.  
  237.         if IsNumeric( cValue ) then
  238.  
  239.             iValue = iValue * 10 + cValue
  240.  
  241.         elseif cValue = "." or cValue = "" then
  242.  
  243.             if iValue < 0 or iValue > 255 then
  244.  
  245.                 DebugPrint kDebugTrace, "Value out of range " & iValue
  246.  
  247.                 exit for
  248.  
  249.             end if
  250.  
  251.             aOctetArray(iDotCount) = iValue
  252.  
  253.             iDotCount = iDotCount + 1
  254.  
  255.             iValue = 0
  256.  
  257.             if cValue = "" then
  258.  
  259.                 exit for
  260.  
  261.             end if
  262.  
  263.         else
  264.  
  265.             DebugPrint kDebugTrace, "Invalid character found " & cValue
  266.  
  267.         end if
  268.  
  269.     next
  270.  
  271.     CreateValueFromOctetString = iDotCount = 4
  272.  
  273. end function
  274.  
  275.  
  276. '
  277. ' List subnet objects.
  278. '
  279. function ListSubnetObjects( )
  280.  
  281.     on error resume next
  282.  
  283.     DebugPrint kDebugTrace, "In the ListSubnetObject"
  284.  
  285.     dim oConfigurationContainer
  286.     dim oSite
  287.     dim oSites
  288.     dim oSubnet
  289.     dim strSiteName
  290.     dim strSubnet
  291.  
  292.     call GetConfigurationContainer( oConfigurationContainer )
  293.  
  294.     DebugPrint kDebugError, "GetConfigurationContainer"
  295.  
  296.     for each oSites In oConfigurationContainer
  297.  
  298.         if oSites.name = "CN=Sites" then
  299.  
  300.             for each oSite in oSites
  301.  
  302.                 if oSite.name = "CN=Subnets" then
  303.  
  304.                     for each oSubnet in oSite
  305.  
  306.                         call GetSiteObjectName( strSiteName, oSubnet.siteObject )
  307.  
  308.                         strSubnet = StripCNFromName( oSubnet.name )
  309.  
  310.                         wscript.echo "Name:", strSubnet, vbTab, "Location:", oSubnet.location, "Site:", strSiteName
  311.  
  312.                     next
  313.  
  314.                 end if
  315.  
  316.             next
  317.  
  318.         end if
  319.  
  320.     next
  321.  
  322.     ListSubnetObjects = Err <> 0
  323.  
  324. end function
  325.  
  326. '
  327. ' Get the site name from a site object name, basically return
  328. ' the CN of the site for the give Site DN
  329. '
  330. sub GetSiteObjectName( ByRef strSiteName, ByRef strSiteObject )
  331.  
  332.     on error resume next
  333.  
  334.     dim oSiteObject
  335.  
  336.     set oSiteObject = GetObject( "LDAP://" & strSiteObject )
  337.  
  338.     strSiteName = StripCNFromName( oSiteObject.name )
  339.  
  340. end sub
  341.  
  342.  
  343. '
  344. ' Function to strip the CN prefix from the given name
  345. '
  346. function StripCNFromName( ByRef strNameWithCN )
  347.  
  348.     StripCNFromName = Mid( strNameWithCN, 4 )
  349.  
  350. end function
  351.  
  352. '
  353. ' Delete subnet object.
  354. '
  355. function DeleteSubnetObject( ByRef strSubnetName )
  356.  
  357.     on error resume next
  358.  
  359.     DebugPrint kDebugTrace, "In the DeleteSubnetObject"
  360.  
  361.     dim oConfigurationContainer
  362.     dim oSubnets
  363.  
  364.     call GetConfigurationContainer( oConfigurationContainer )
  365.  
  366.     DebugPrint kDebugError, "GetConfigurationContainer"
  367.  
  368.     set oSubnets = oConfigurationContainer.GetObject("subnetContainer", "CN=Subnets,CN=Sites")
  369.  
  370.     DebugPrint kDebugError, "GetSubnetContainer"
  371.  
  372.     oSubnets.Delete "subnet", "CN=" & strSubnetName
  373.  
  374.     DebugPrint kDebugError, "Delete subnet"
  375.  
  376.     if Err <> 0 then
  377.         wscript.echo "Failure - deleting subnet " & strSubnetName & " error code " & hex(err)
  378.     else
  379.         wscript.echo "Success - deleting subnet " & strSubnetName & ""
  380.     end if
  381.  
  382.     DeleteSubnetObject = Err <> 0
  383.  
  384. end function
  385.  
  386.  
  387. '
  388. ' Delete subnet object.
  389. '
  390. function DeleteAllSubnetObjects( )
  391.  
  392.     on error resume next
  393.  
  394.     DebugPrint kDebugTrace, "In the DeleteAllSubnetObjects"
  395.  
  396.     dim oConfigurationContainer
  397.     dim oSite
  398.     dim oSites
  399.     dim oSubnet
  400.  
  401.     call GetConfigurationContainer( oConfigurationContainer )
  402.  
  403.     DebugPrint kDebugError, "GetConfigurationContainer"
  404.  
  405.     for each oSites In oConfigurationContainer
  406.  
  407.         if oSites.name = "CN=Sites" then
  408.  
  409.             for each oSite in oSites
  410.  
  411.                 if oSite.name = "CN=Subnets" then
  412.  
  413.                     for each oSubnet in oSite
  414.  
  415.                         DeleteSubnetObject( StripCNFromName( oSubnet.name ) )
  416.  
  417.                     next
  418.  
  419.                 end if
  420.  
  421.             next
  422.  
  423.         end if
  424.  
  425.     next
  426.  
  427.     DeleteAllSubnetObects = True
  428.  
  429. end function
  430.  
  431.  
  432. '
  433. ' Creates a subnet object in the current domain.
  434. '
  435. function CreateSubnetObject( ByRef oSubnet, ByRef strSubnetName, ByRef strSiteName, ByRef strLocationName )
  436.  
  437.     on error resume next
  438.  
  439.     DebugPrint kDebugTrace, "In the CreateSubnetObject"
  440.     DebugPrint kDebugTrace, "Subnet: " & strSubnetName & " Site: " & strSiteName & " Location: " & strLocationName
  441.  
  442.     dim oConfigurationContainer
  443.     dim oSubnets
  444.     dim strFullSiteName
  445.  
  446.     call GetConfigurationContainer( oConfigurationContainer )
  447.  
  448.     DebugPrint kDebugError, "GetConfigurationContainer"
  449.  
  450.     set oSubnets = oConfigurationContainer.GetObject( "subnetContainer", "CN=Subnets,CN=Sites" )
  451.  
  452.     DebugPrint kDebugError, "Get Subnet Containter"
  453.  
  454.     set oSubnet = oSubnets.Create("subnet", "cn=" & strSubnetName )
  455.  
  456.     DebugPrint kDebugError, "Create Subnet Object"
  457.  
  458.     if strSiteName <> Empty then
  459.  
  460.         if CreateFullSiteName( strSiteName, strFullSiteName ) = True then
  461.  
  462.             DebugPrint kDebugTrace, "Site Name " & strFullSiteName
  463.  
  464.             oSubnet.put "siteObject", strFullSiteName
  465.  
  466.         end if
  467.  
  468.     end if
  469.  
  470.     if strLocationName <> Empty then
  471.  
  472.         oSubnet.put "location", strLocationName
  473.  
  474.     end if
  475.  
  476.     oSubnet.SetInfo
  477.  
  478.     DebugPrint kDebugError, "Setinfo on Subnet Object"
  479.  
  480.     if Err <> 0 then
  481.         wscript.echo "Failure - adding subnet " & strSubnetName & " error code " & hex(err)
  482.     else
  483.         wscript.echo "Success - adding subnet " & strSubnetName & ""
  484.     end if
  485.  
  486.     CreateSubnetObject = Err <> 0
  487.  
  488. end function
  489.  
  490. '
  491. ' Create the fully qualified site name of the site name does not have a cn= prefix.
  492. '
  493. function CreateFullSiteName( ByRef strSiteName, ByRef strFullSiteName )
  494.  
  495.     on error resume next
  496.  
  497.     DebugPrint kDebugTrace, "In the CreateFullSiteName"
  498.  
  499.     if UCase( Left( strSiteName, 3 ) ) <> "CN=" then
  500.  
  501.         dim RootDSE
  502.  
  503.         set RootDSE = GetObject("LDAP://RootDSE")
  504.  
  505.         if Err = 0 then
  506.  
  507.             strFullSiteName = "CN=" & strSiteName & ",CN=Sites," & RootDSE.get("configurationNamingContext")
  508.  
  509.         end if
  510.  
  511.     else
  512.  
  513.         strFullSiteName = strSiteName
  514.  
  515.     end if
  516.  
  517.     CreateFullSiteName = strFullSiteName <> Empty
  518.  
  519. end function
  520.  
  521.  
  522.  
  523. '
  524. ' Get the configuration container object
  525. '
  526. function GetConfigurationContainer( ByRef oConfigurationContainer )
  527.  
  528.     on error resume next
  529.  
  530.     DebugPrint kDebugTrace, "In the GetConfigurationContainer"
  531.  
  532.     dim RootDSE
  533.     dim strConfigurationContainer
  534.  
  535.     set RootDSE = GetObject("LDAP://RootDSE")
  536.  
  537.     DebugPrint kDebugError, "GetObject of RootDSE failed with "
  538.  
  539.     set oConfigurationContainer = GetObject("LDAP://" & RootDSE.get("configurationNamingContext"))
  540.  
  541.     DebugPrint kDebugError, "GetConfigurationContainer"
  542.  
  543.     GetConfigurationContainer = Err <> 0
  544.  
  545. end function
  546.  
  547. '
  548. ' Debug dispaly helper fuction
  549. '
  550. sub DebugPrint( ByVal Flags, ByRef String )
  551.  
  552.     if gDebugFlag = True then
  553.  
  554.         if Flags = kDebugTrace then
  555.  
  556.             WScript.echo String
  557.  
  558.         end if
  559.  
  560.         if Flags = kDebugError then
  561.  
  562.             if Err <> 0 then
  563.  
  564.                 WScript.echo String & " Failed with " & Hex( Err )
  565.  
  566.             end if
  567.  
  568.         end if
  569.  
  570.     end if
  571.  
  572. end sub
  573.  
  574. '
  575. ' Parse the command line into it's components
  576. '
  577. function ParseCommandLine( ByRef iAction, ByRef strSubnet, ByRef strSite, ByRef strLocation, ByRef strIpAddress, ByRef strSubnetMask )
  578.  
  579.     DebugPrint kDebugTrace, "In the ParseCommandLine"
  580.  
  581.     dim oArgs
  582.     dim strArg
  583.     dim i
  584.  
  585.     set oArgs = Wscript.Arguments
  586.  
  587.     while i < oArgs.Count
  588.  
  589.         select case oArgs(i)
  590.  
  591.             Case "-a"
  592.                 iAction = kActionAdd
  593.  
  594.             Case "-d"
  595.                 iAction = kActionDelete
  596.  
  597.             Case "-l"
  598.                 iAction = kActionList
  599.  
  600.             Case "-c"
  601.                 iAction = kActionCalculate
  602.  
  603.             Case "-n"
  604.                 i = i + 1
  605.                 strSubnet = oArgs(i)
  606.  
  607.             Case "-s"
  608.                 i = i + 1
  609.                 strSite = oArgs(i)
  610.  
  611.             Case "-p"
  612.                 i = i + 1
  613.                 strLocation = oArgs(i)
  614.  
  615.             Case "-i"
  616.                 i = i + 1
  617.                 strIpAddress = oArgs(i)
  618.  
  619.             Case "-m"
  620.                 i = i + 1
  621.                 strSubnetMask = oArgs(i)
  622.  
  623.             Case "-?"
  624.                 Usage( True )
  625.                 exit function
  626.  
  627.             Case Else
  628.                 Usage( True )
  629.                 exit function
  630.  
  631.         End Select
  632.  
  633.         i = i + 1
  634.  
  635.     wend
  636.  
  637.     DebugPrint kDebugTrace, "ParseCommandLine Result: " & iAction & " " & strSubnet & " " & strSite & " " & strLocation
  638.  
  639.     ParseCommandLine = 0
  640.  
  641. end  function
  642.  
  643.  
  644. '
  645. ' Display command usage.
  646. '
  647. sub Usage( ByVal bExit )
  648.  
  649.     wscript.echo "Usage: subnet_op [-acdl?] [-n subnet-name] [-s site-name] [-p location]"
  650.     wscript.echo "                 [-i ip address] [-m subnet mask]"
  651.     wscript.echo "Arguments:"
  652.     wscript.echo "-a     - add the specfied subnet object"
  653.     wscript.echo "-c     - calculate subnet object name from ip address and subnet mask"
  654.     wscript.echo "-d     - delete the specified subnet object, use 'all' to deleted all subnets"
  655.     wscript.echo "-i     - specifies the ip address"
  656.     wscript.echo "-l     - list all the subnet objects"
  657.     wscript.echo "-m     - specifies the subnet mask"
  658.     wscript.echo "-n     - specifies the subnet name"
  659.     wscript.echo "-p     - specifies the location string for subnet object"
  660.     wscript.echo "-s     - specifies the site for the subnet object"
  661.     wscript.echo "-?     - display command usage"
  662.     wscript.echo ""
  663.     wscript.echo "Examples:"
  664.     wscript.echo "subnet_op -l"
  665.     wscript.echo "subnet_op -a -n 1.0.0.0/8"
  666.     wscript.echo "subnet_op -a -n 1.0.0.0/8 -p USA/RED/27N"
  667.     wscript.echo "subnet_op -a -n 1.0.0.0/8 -p USA/RED/27N -s Default-First-Site-Name"
  668.     wscript.echo "subnet_op -d -n 1.0.0.0/8"
  669.     wscript.echo "subnet_op -d -n all"
  670.     wscript.echo "subnet_op -c -i 157.59.16.32 -m 255.255.252.0"
  671.  
  672.     if bExit <> 0 then
  673.         wscript.quit(1)
  674.     end if
  675.  
  676. end sub
  677.  
  678. '
  679. ' Determines which program is used to run this script. 
  680. ' Returns true if the script host is cscript.exe
  681. '
  682. function IsHostCscript()
  683.  
  684.     on error resume next
  685.     
  686.     dim strFullName 
  687.     dim strCommand 
  688.     dim i, j 
  689.     dim bReturn
  690.     
  691.     bReturn = false
  692.     
  693.     strFullName = WScript.FullName
  694.     
  695.     i = InStr(1, strFullName, ".exe", 1)
  696.     
  697.     if i <> 0 then
  698.         
  699.         j = InStrRev(strFullName, "\", i, 1)
  700.         
  701.         if j <> 0 then
  702.             
  703.             strCommand = Mid(strFullName, j+1, i-j-1)
  704.             
  705.             if LCase(strCommand) = "cscript" then
  706.             
  707.                 bReturn = true  
  708.             
  709.             end if    
  710.                 
  711.         end if
  712.         
  713.     end if
  714.     
  715.     if Err <> 0 then
  716.     
  717.         call wscript.echo("Error 0x" & hex(Err.Number) & " occurred. " & Err.Description _
  718.                           & ". " & vbCRLF & "The scripting host could not be determined.")       
  719.         
  720.     end if
  721.     
  722.     IsHostCscript = bReturn
  723.  
  724. end function
  725.