Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

724 lines
16 KiB

  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. option explicit
  17. '
  18. ' Debugging trace flags.
  19. '
  20. const kDebugTrace = 1
  21. const kDebugError = 2
  22. dim gDebugFlag
  23. '
  24. ' To enable debug output trace message
  25. ' Change the following variable to True.
  26. '
  27. gDebugFlag = False
  28. '
  29. ' Messages to be displayed if the scripting host is not cscript
  30. '
  31. const kMessage1 = "Please run this script using CScript."
  32. const kMessage2 = "This can be achieved by"
  33. const kMessage3 = "1. Using ""CScript script.vbs arguments"" or"
  34. const kMessage4 = "2. Changing the default Windows Scripting Host to CScript"
  35. const kMessage5 = " using ""CScript //H:CScript //S"" and running the script "
  36. const kMessage6 = " ""script.vbs arguments""."
  37. '
  38. ' Operation action values.
  39. '
  40. const kActionAdd = 0
  41. const kActionDelete = 1
  42. const kActionList = 2
  43. const kActionCalculate = 3
  44. const kActionUnknown = 4
  45. main
  46. '
  47. ' Main execution start here
  48. '
  49. sub main
  50. on error resume next
  51. dim strSubnet
  52. dim strSite
  53. dim strLocation
  54. dim strIpAddress
  55. dim strSubnetMask
  56. dim iAction
  57. dim oSubnet
  58. dim iRetval
  59. dim oArgs
  60. '
  61. ' Abort if the host is not cscript
  62. '
  63. if not IsHostCscript() then
  64. call wscript.echo(kMessage1 & vbCRLF & kMessage2 & vbCRLF & _
  65. kMessage3 & vbCRLF & kMessage4 & vbCRLF & _
  66. kMessage5 & vbCRLF & kMessage6 & vbCRLF)
  67. wscript.quit
  68. end if
  69. iAction = kActionUnknown
  70. iRetval = ParseCommandLine( iAction, strSubnet, strSite, strLocation, strIpAddress, strSubnetMask )
  71. if iRetval = 0 then
  72. select case iAction
  73. case kActionAdd
  74. iRetval = CreateSubnetObject( oSubnet, strSubnet, strSite, strLocation )
  75. case kActionDelete
  76. if strSubnet = "all" then
  77. iRetval = DeleteAllSubnetObjects( )
  78. else
  79. iRetval = DeleteSubnetObject( strSubnet )
  80. end if
  81. case kActionList
  82. iRetval = ListSubnetObjects( )
  83. case kActionCalculate
  84. iRetval = CalculateSubnetObject( strIpAddress, strSubnetMask )
  85. case else
  86. Usage( True )
  87. exit sub
  88. end select
  89. end if
  90. end sub
  91. '
  92. ' Calculate the subnet object name an IP address and a subnet mask
  93. '
  94. function CalculateSubnetObject( ByRef strIpAddress, ByRef strSubnetMask )
  95. on error resume next
  96. DebugPrint kDebugTrace, "In the CalculateSubnetObject"
  97. Dim aIpAddressOctetArray(4)
  98. Dim aSubnetMaskOctetArray(4)
  99. Dim strSubnetObjectName
  100. if CreateValueFromOctetString( strIpAddress, aIpAddressOctetArray ) = False then
  101. wscript.echo "Invalid IP Address, must be of the form ddd.ddd.ddd.ddd, e.g. 157.41.50.2"
  102. CalculateSubnetObject = False
  103. exit function
  104. end if
  105. if CreateValueFromOctetString( strSubnetMask, aSubnetMaskOctetArray ) = False then
  106. wscript.echo "Invalid Subnet mask, must be of the form ddd.ddd.ddd.ddd, e.g. 255.255.252.0"
  107. CalculateSubnetObject = False
  108. exit function
  109. end if
  110. DebugPrint kDebugTrace, "Ip Octet Value " & aIpAddressOctetArray(0) & "." & aIpAddressOctetArray(1) & "." & aIpAddressOctetArray(2) & "." & aIpAddressOctetArray(3)
  111. DebugPrint kDebugTrace, "Mask Octet Value " & aSubnetMaskOctetArray(0) & "." & aSubnetMaskOctetArray(1) & "." & aSubnetMaskOctetArray(2) & "." & aSubnetMaskOctetArray(3)
  112. if ConvertToSubnetName( aIpAddressOctetArray, aSubnetMaskOctetArray, strSubnetObjectName ) = True then
  113. wscript.echo "Subnet Object Name:", strSubnetObjectName
  114. CalculateSubnetObject = True
  115. else
  116. wscript.echo "Unable to convert IP address and subnet mask to subnet object name"
  117. CalculateSubnetObject = False
  118. end if
  119. end function
  120. '
  121. ' Convert the given ip address and subnet mask to a DS object name.
  122. ' The algorithm is to take the ip address and it with the subnet mask
  123. ' this is the subnet then tack on a slash and place the number of bits
  124. ' in the subnet mask at the end. For example ip address of 157.59.16.2
  125. ' subnet mask 255.255.252.0 results in a subnet object name of 157.59.0.0/22
  126. '
  127. function ConvertToSubnetName( ByRef aIpOctetArray, ByRef aSubnetOctetArray, ByRef strSubnet )
  128. on error resume next
  129. DebugPrint kDebugTrace, "In the ConvertToSubnetObject"
  130. Dim iVal0
  131. Dim iVal1
  132. Dim iVal2
  133. Dim iVal3
  134. Dim iBits
  135. Dim i
  136. Dim j
  137. iVal0 = aIpOctetArray(0) and aSubnetOctetArray(0)
  138. iVal1 = aIpOctetArray(1) and aSubnetOctetArray(1)
  139. iVal2 = aIpOctetArray(2) and aSubnetOctetArray(2)
  140. iVal3 = aIpOctetArray(3) and aSubnetOctetArray(3)
  141. for i = 0 to 3
  142. for j = 0 to 7
  143. if (aSubnetOctetArray(i) and (2 ^ j)) <> 0 then
  144. iBits = iBits + 1
  145. end if
  146. next
  147. next
  148. strSubnet = iVal0 & "." & iVal1 & "." & iVal2 & "." & iVal3 & "/" & iBits
  149. ConvertToSubnetName = True
  150. end function
  151. '
  152. ' Converts an octet string to an array of octet values. For example
  153. ' this function when given string 157.59.161.2 will return an array with
  154. ' the followin values. A(0) = 157, A(1) = 59, A(2) = 161, A(3) = 2
  155. '
  156. function CreateValueFromOctetString( ByRef strOctet, ByRef aOctetArray )
  157. on error resume next
  158. DebugPrint kDebugTrace, "In the CreateValueFromOctetString"
  159. Dim i
  160. Dim iDotCount
  161. Dim cValue
  162. Dim iValue
  163. for i = 0 to 32
  164. cValue = Mid( strOctet, i, 1 )
  165. if IsNumeric( cValue ) then
  166. iValue = iValue * 10 + cValue
  167. elseif cValue = "." or cValue = "" then
  168. if iValue < 0 or iValue > 255 then
  169. DebugPrint kDebugTrace, "Value out of range " & iValue
  170. exit for
  171. end if
  172. aOctetArray(iDotCount) = iValue
  173. iDotCount = iDotCount + 1
  174. iValue = 0
  175. if cValue = "" then
  176. exit for
  177. end if
  178. else
  179. DebugPrint kDebugTrace, "Invalid character found " & cValue
  180. end if
  181. next
  182. CreateValueFromOctetString = iDotCount = 4
  183. end function
  184. '
  185. ' List subnet objects.
  186. '
  187. function ListSubnetObjects( )
  188. on error resume next
  189. DebugPrint kDebugTrace, "In the ListSubnetObject"
  190. dim oConfigurationContainer
  191. dim oSite
  192. dim oSites
  193. dim oSubnet
  194. dim strSiteName
  195. dim strSubnet
  196. call GetConfigurationContainer( oConfigurationContainer )
  197. DebugPrint kDebugError, "GetConfigurationContainer"
  198. for each oSites In oConfigurationContainer
  199. if oSites.name = "CN=Sites" then
  200. for each oSite in oSites
  201. if oSite.name = "CN=Subnets" then
  202. for each oSubnet in oSite
  203. call GetSiteObjectName( strSiteName, oSubnet.siteObject )
  204. strSubnet = StripCNFromName( oSubnet.name )
  205. wscript.echo "Name:", strSubnet, vbTab, "Location:", oSubnet.location, "Site:", strSiteName
  206. next
  207. end if
  208. next
  209. end if
  210. next
  211. ListSubnetObjects = Err <> 0
  212. end function
  213. '
  214. ' Get the site name from a site object name, basically return
  215. ' the CN of the site for the give Site DN
  216. '
  217. sub GetSiteObjectName( ByRef strSiteName, ByRef strSiteObject )
  218. on error resume next
  219. dim oSiteObject
  220. set oSiteObject = GetObject( "LDAP://" & strSiteObject )
  221. strSiteName = StripCNFromName( oSiteObject.name )
  222. end sub
  223. '
  224. ' Function to strip the CN prefix from the given name
  225. '
  226. function StripCNFromName( ByRef strNameWithCN )
  227. StripCNFromName = Mid( strNameWithCN, 4 )
  228. end function
  229. '
  230. ' Delete subnet object.
  231. '
  232. function DeleteSubnetObject( ByRef strSubnetName )
  233. on error resume next
  234. DebugPrint kDebugTrace, "In the DeleteSubnetObject"
  235. dim oConfigurationContainer
  236. dim oSubnets
  237. call GetConfigurationContainer( oConfigurationContainer )
  238. DebugPrint kDebugError, "GetConfigurationContainer"
  239. set oSubnets = oConfigurationContainer.GetObject("subnetContainer", "CN=Subnets,CN=Sites")
  240. DebugPrint kDebugError, "GetSubnetContainer"
  241. oSubnets.Delete "subnet", "CN=" & strSubnetName
  242. DebugPrint kDebugError, "Delete subnet"
  243. if Err <> 0 then
  244. wscript.echo "Failure - deleting subnet " & strSubnetName & " error code " & hex(err)
  245. else
  246. wscript.echo "Success - deleting subnet " & strSubnetName & ""
  247. end if
  248. DeleteSubnetObject = Err <> 0
  249. end function
  250. '
  251. ' Delete subnet object.
  252. '
  253. function DeleteAllSubnetObjects( )
  254. on error resume next
  255. DebugPrint kDebugTrace, "In the DeleteAllSubnetObjects"
  256. dim oConfigurationContainer
  257. dim oSite
  258. dim oSites
  259. dim oSubnet
  260. call GetConfigurationContainer( oConfigurationContainer )
  261. DebugPrint kDebugError, "GetConfigurationContainer"
  262. for each oSites In oConfigurationContainer
  263. if oSites.name = "CN=Sites" then
  264. for each oSite in oSites
  265. if oSite.name = "CN=Subnets" then
  266. for each oSubnet in oSite
  267. DeleteSubnetObject( StripCNFromName( oSubnet.name ) )
  268. next
  269. end if
  270. next
  271. end if
  272. next
  273. DeleteAllSubnetObects = True
  274. end function
  275. '
  276. ' Creates a subnet object in the current domain.
  277. '
  278. function CreateSubnetObject( ByRef oSubnet, ByRef strSubnetName, ByRef strSiteName, ByRef strLocationName )
  279. on error resume next
  280. DebugPrint kDebugTrace, "In the CreateSubnetObject"
  281. DebugPrint kDebugTrace, "Subnet: " & strSubnetName & " Site: " & strSiteName & " Location: " & strLocationName
  282. dim oConfigurationContainer
  283. dim oSubnets
  284. dim strFullSiteName
  285. call GetConfigurationContainer( oConfigurationContainer )
  286. DebugPrint kDebugError, "GetConfigurationContainer"
  287. set oSubnets = oConfigurationContainer.GetObject( "subnetContainer", "CN=Subnets,CN=Sites" )
  288. DebugPrint kDebugError, "Get Subnet Containter"
  289. set oSubnet = oSubnets.Create("subnet", "cn=" & strSubnetName )
  290. DebugPrint kDebugError, "Create Subnet Object"
  291. if strSiteName <> Empty then
  292. if CreateFullSiteName( strSiteName, strFullSiteName ) = True then
  293. DebugPrint kDebugTrace, "Site Name " & strFullSiteName
  294. oSubnet.put "siteObject", strFullSiteName
  295. end if
  296. end if
  297. if strLocationName <> Empty then
  298. oSubnet.put "location", strLocationName
  299. end if
  300. oSubnet.SetInfo
  301. DebugPrint kDebugError, "Setinfo on Subnet Object"
  302. if Err <> 0 then
  303. wscript.echo "Failure - adding subnet " & strSubnetName & " error code " & hex(err)
  304. else
  305. wscript.echo "Success - adding subnet " & strSubnetName & ""
  306. end if
  307. CreateSubnetObject = Err <> 0
  308. end function
  309. '
  310. ' Create the fully qualified site name of the site name does not have a cn= prefix.
  311. '
  312. function CreateFullSiteName( ByRef strSiteName, ByRef strFullSiteName )
  313. on error resume next
  314. DebugPrint kDebugTrace, "In the CreateFullSiteName"
  315. if UCase( Left( strSiteName, 3 ) ) <> "CN=" then
  316. dim RootDSE
  317. set RootDSE = GetObject("LDAP://RootDSE")
  318. if Err = 0 then
  319. strFullSiteName = "CN=" & strSiteName & ",CN=Sites," & RootDSE.get("configurationNamingContext")
  320. end if
  321. else
  322. strFullSiteName = strSiteName
  323. end if
  324. CreateFullSiteName = strFullSiteName <> Empty
  325. end function
  326. '
  327. ' Get the configuration container object
  328. '
  329. function GetConfigurationContainer( ByRef oConfigurationContainer )
  330. on error resume next
  331. DebugPrint kDebugTrace, "In the GetConfigurationContainer"
  332. dim RootDSE
  333. dim strConfigurationContainer
  334. set RootDSE = GetObject("LDAP://RootDSE")
  335. DebugPrint kDebugError, "GetObject of RootDSE failed with "
  336. set oConfigurationContainer = GetObject("LDAP://" & RootDSE.get("configurationNamingContext"))
  337. DebugPrint kDebugError, "GetConfigurationContainer"
  338. GetConfigurationContainer = Err <> 0
  339. end function
  340. '
  341. ' Debug dispaly helper fuction
  342. '
  343. sub DebugPrint( ByVal Flags, ByRef String )
  344. if gDebugFlag = True then
  345. if Flags = kDebugTrace then
  346. WScript.echo String
  347. end if
  348. if Flags = kDebugError then
  349. if Err <> 0 then
  350. WScript.echo String & " Failed with " & Hex( Err )
  351. end if
  352. end if
  353. end if
  354. end sub
  355. '
  356. ' Parse the command line into it's components
  357. '
  358. function ParseCommandLine( ByRef iAction, ByRef strSubnet, ByRef strSite, ByRef strLocation, ByRef strIpAddress, ByRef strSubnetMask )
  359. DebugPrint kDebugTrace, "In the ParseCommandLine"
  360. dim oArgs
  361. dim strArg
  362. dim i
  363. set oArgs = Wscript.Arguments
  364. while i < oArgs.Count
  365. select case oArgs(i)
  366. Case "-a"
  367. iAction = kActionAdd
  368. Case "-d"
  369. iAction = kActionDelete
  370. Case "-l"
  371. iAction = kActionList
  372. Case "-c"
  373. iAction = kActionCalculate
  374. Case "-n"
  375. i = i + 1
  376. strSubnet = oArgs(i)
  377. Case "-s"
  378. i = i + 1
  379. strSite = oArgs(i)
  380. Case "-p"
  381. i = i + 1
  382. strLocation = oArgs(i)
  383. Case "-i"
  384. i = i + 1
  385. strIpAddress = oArgs(i)
  386. Case "-m"
  387. i = i + 1
  388. strSubnetMask = oArgs(i)
  389. Case "-?"
  390. Usage( True )
  391. exit function
  392. Case Else
  393. Usage( True )
  394. exit function
  395. End Select
  396. i = i + 1
  397. wend
  398. DebugPrint kDebugTrace, "ParseCommandLine Result: " & iAction & " " & strSubnet & " " & strSite & " " & strLocation
  399. ParseCommandLine = 0
  400. end function
  401. '
  402. ' Display command usage.
  403. '
  404. sub Usage( ByVal bExit )
  405. wscript.echo "Usage: subnet_op [-acdl?] [-n subnet-name] [-s site-name] [-p location]"
  406. wscript.echo " [-i ip address] [-m subnet mask]"
  407. wscript.echo "Arguments:"
  408. wscript.echo "-a - add the specfied subnet object"
  409. wscript.echo "-c - calculate subnet object name from ip address and subnet mask"
  410. wscript.echo "-d - delete the specified subnet object, use 'all' to deleted all subnets"
  411. wscript.echo "-i - specifies the ip address"
  412. wscript.echo "-l - list all the subnet objects"
  413. wscript.echo "-m - specifies the subnet mask"
  414. wscript.echo "-n - specifies the subnet name"
  415. wscript.echo "-p - specifies the location string for subnet object"
  416. wscript.echo "-s - specifies the site for the subnet object"
  417. wscript.echo "-? - display command usage"
  418. wscript.echo ""
  419. wscript.echo "Examples:"
  420. wscript.echo "subnet_op -l"
  421. wscript.echo "subnet_op -a -n 1.0.0.0/8"
  422. wscript.echo "subnet_op -a -n 1.0.0.0/8 -p USA/RED/27N"
  423. wscript.echo "subnet_op -a -n 1.0.0.0/8 -p USA/RED/27N -s Default-First-Site-Name"
  424. wscript.echo "subnet_op -d -n 1.0.0.0/8"
  425. wscript.echo "subnet_op -d -n all"
  426. wscript.echo "subnet_op -c -i 157.59.16.32 -m 255.255.252.0"
  427. if bExit <> 0 then
  428. wscript.quit(1)
  429. end if
  430. end sub
  431. '
  432. ' Determines which program is used to run this script.
  433. ' Returns true if the script host is cscript.exe
  434. '
  435. function IsHostCscript()
  436. on error resume next
  437. dim strFullName
  438. dim strCommand
  439. dim i, j
  440. dim bReturn
  441. bReturn = false
  442. strFullName = WScript.FullName
  443. i = InStr(1, strFullName, ".exe", 1)
  444. if i <> 0 then
  445. j = InStrRev(strFullName, "\", i, 1)
  446. if j <> 0 then
  447. strCommand = Mid(strFullName, j+1, i-j-1)
  448. if LCase(strCommand) = "cscript" then
  449. bReturn = true
  450. end if
  451. end if
  452. end if
  453. if Err <> 0 then
  454. call wscript.echo("Error 0x" & hex(Err.Number) & " occurred. " & Err.Description _
  455. & ". " & vbCRLF & "The scripting host could not be determined.")
  456. end if
  457. IsHostCscript = bReturn
  458. end function