⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ms2isc.pl

📁 dhcp-3.1.0.tar.gz。DHCP源代码
💻 PL
📖 第 1 页 / 共 2 页
字号:
#     $Scopes{<subnet>}{winstype} => integer
#     $Scopes{<subnet>}{leaseduration} => integer

	my ($RegVal, @Subnets, @Router, $SubnetName, $SubnetComment, @SubnetOptions, @SRouter, @SDNSServers, @SDomainname, @SWINSservers, @SNetBIOS, @SLeaseDuration, @SSubnetState, @SExclusionRanges, @SSubnetAddress, @SSubnetMask, @SFirstAddress, $SStartAddress, $SEndAddress, @InclusionRanges, @SBroadcastAddress);

	print "Getting list of subnets\n";
	if (Registry::GetRegSubkeyList ("\\\\$Server\\HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Services\\DHCPServer\\Configuration\\Subnets", \@Subnets)) {
		die "Unable to obtain a list of subnets from the server!\n";
	}

	for (my $i=0; $i<=$#Subnets; $i++) {
		print "\t Fetching Subnet $Subnets[$i] (",$i+1, "/", $#Subnets+1, "): ";

		print ".";
		if (!Registry::GetRegSubkeyList ("\\\\$Server\\HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Services\\DHCPServer\\Configuration\\Subnets\\$Subnets[$i]\\IpRanges", \@SFirstAddress)) {
			# Don't know why MS has a tree for this, but as far
			# as I can tell, only one subtree will ever come out of
			# this, so I'm skipping the 'for' loop
		
			print ".";
			if (!Registry::GetRegKeyVal ("\\\\$Server\\HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Services\\DHCPServer\\Configuration\\Subnets\\$Subnets[$i]\\IpRanges\\$SFirstAddress[0]\\StartAddress", \$RegVal)) {
				$SStartAddress = $RegVal;
			}
			print ".";
			if (!Registry::GetRegKeyVal ("\\\\$Server\\HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Services\\DHCPServer\\Configuration\\Subnets\\$Subnets[$i]\\IpRanges\\$SFirstAddress[0]\\EndAddress", \$RegVal)) {
				$SEndAddress = $RegVal;
			}
# print "\n\tInclusion Range: ", Registry::ExtractIp($SStartAddress), " - ", Registry::ExtractIp($SEndAddress),"\n";
	
		} else {
			die "\n\n# Error Getting Inclusion Range FirstAddress!!!\n\n";
		}

		if (!Registry::GetRegKeyVal ("\\\\$Server\\HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Services\\DHCPServer\\Configuration\\Subnets\\$Subnets[$i]\\ExcludedIpRanges", \$RegVal)) {
			@SExclusionRanges = Registry::ExtractExclusionRanges($RegVal);

#			for (my $j=2; $j<=$#SExclusionRanges; $j+=2) {
#				if (unpack("L",$SExclusionRanges[$j]) < unpack("L",$SExclusionRanges[$j-2])) {
#					print ("\n******** Subnet exclusion ranges out of order ********\n");
#				}
#			}

			@SExclusionRanges = sort(@SExclusionRanges);

#		print "\n\tExclusion Ranges: ";
#		for (my $j=0; $j<=$#SExclusionRanges; $j+=2) {
#			print "\n\t\t",Registry::ExtractIp($SExclusionRanges[$j])," - ",Registry::ExtractIp($SExclusionRanges[$j+1]);
#		}

		}
		@InclusionRanges = FindInclusionRanges ($SStartAddress, $SEndAddress, @SExclusionRanges);

		print ".";
		if (!Registry::GetRegKeyVal ("\\\\$Server\\HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Services\\DHCPServer\\Configuration\\Subnets\\$Subnets[$i]\\SubnetName", \$RegVal)) {
			$SubnetName = $RegVal;
#		print "\n\tSubnetName: $SubnetName";
		}

		print ".";
		if (!Registry::GetRegKeyVal ("\\\\$Server\\HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Services\\DHCPServer\\Configuration\\Subnets\\$Subnets[$i]\\SubnetComment", \$RegVal)) {
			$SubnetComment = $RegVal;
#		print "\n\tSubnetComment: $SubnetComment";
		}
		print ".";
		if (!Registry::GetRegKeyVal ("\\\\$Server\\HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Services\\DHCPServer\\Configuration\\Subnets\\$Subnets[$i]\\SubnetAddress", \$RegVal)) {
			@SSubnetAddress = Registry::ExtractIp($RegVal);
#		print "\n\tSubnetAddress: $SSubnetAddress[0]";
		}
		print ".";
		if (!Registry::GetRegKeyVal ("\\\\$Server\\HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Services\\DHCPServer\\Configuration\\Subnets\\$Subnets[$i]\\SubnetMask", \$RegVal)) {
			@SSubnetMask = Registry::ExtractIp($RegVal);
#		print "\n\tSubnetMask: $SSubnetMask[0]";
		}

		print ".";
		if (!Registry::GetRegKeyVal ("\\\\$Server\\HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Services\\DHCPServer\\Configuration\\Subnets\\$Subnets[$i]\\SubnetState", \$RegVal)) {
			@SSubnetState = Registry::ExtractHex ($RegVal);
#		print "\n\tSubnetState = $SSubnetState[0]";
		}

		$Scopes{$Subnets[$i]}{disable} = hex($SSubnetState[0]) ? 1 : 0;
		$Scopes{$Subnets[$i]}{mask} = $SSubnetMask[0];
		$Scopes{$Subnets[$i]}{name} = $SubnetName;
		$Scopes{$Subnets[$i]}{comment} = $SubnetComment;
		for (my $r=0; $r<=$#InclusionRanges; $r++) {
			$Scopes{$Subnets[$i]}{ranges}[$r] = Registry::ExtractIp($InclusionRanges[$r]);
		}

################## Get scope options

		my (@SubnetOptionsList);

		print "\n\t\tOptions:";
		if (Registry::GetRegSubkeyList ("\\\\$Server\\HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Services\\DHCPServer\\Configuration\\Subnets\\$Subnets[$i]\\SubnetOptions", \@SubnetOptionsList)) {
			die "Unable to get subnet options list for $Subnets[$i]!\n";
		}

		for (my $j=0; $j<=$#SubnetOptionsList; $j++) {
			print ".";
			if (!Registry::GetRegKeyVal ("\\\\$Server\\HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Services\\DHCPServer\\Configuration\\Subnets\\$Subnets[$i]\\SubnetOptions\\$SubnetOptionsList[$j]\\OptionValue", \$RegVal)) {
				for ($SubnetOptionsList[$j]) {
					/003/ and do {
#						@SRouter = Registry::ExtractOptionIps($RegVal);
						$Scopes{$Subnets[$i]}{routers} = [Registry::ExtractOptionIps($RegVal)];
						last;
					};
					/006/ and do {
						@SDNSServers = Registry::ExtractOptionIps($RegVal);
						for (my $d=0; $d<=$#SDNSServers; $d++) {
							my ($ipname, $rest) = gethostbyaddr(pack("C4", split(/\./, $SDNSServers[$d])), &AF_INET);
							$Scopes{$Subnets[$i]}{dnses}[$d] = $ipname ? $ipname : $SDNSServers[$d];
		}
						last;
					};
					/015/ and do { 
						@SDomainname = Registry::ExtractOptionStrings($RegVal);
						$Scopes{$Subnets[$i]}{domain} = $SDomainname[0];
						last;
					};
					/028/ and do {
						@SBroadcastAddress = Registry::ExtractOptionIps($RegVal);
						$Scopes{$Subnets[$i]}{broadcast} = $SBroadcastAddress[0];
						last;
					};
					/044/ and do {
						@SWINSservers = Registry::ExtractOptionIps($RegVal);
						for (my $w=0; $w<=$#SWINSservers; $w++) {
							my ($ipname, $rest) = gethostbyaddr(pack("C4", split(/\./, $SWINSservers[$w])), &AF_INET);
							$Scopes{$Subnets[$i]}{winses}[$w] = $ipname ? $ipname : $SWINSservers[$w];
						}
						last;
					};
					/046/ and do {
						@SNetBIOS = Registry::ExtractOptionHex($RegVal);
						$Scopes{$Subnets[$i]}{winstype} = hex($SNetBIOS[0]);
						last;
					};
					/051/ and do {
						@SLeaseDuration = Registry::ExtractOptionHex($RegVal);
						$Scopes{$Subnets[$i]}{leaseduration} = hex($SLeaseDuration[0]);
						last;
					};
					die "This program does not recognize subnet option \#$SubnetOptionsList[$j] yet!\n"
				}
			} else {
					die "Unable to obtain option SubnetOptionsList[$j] from $Subnets[$i], most likely a registry problem!\n"
			}
		}
		print "\n";
	}

	return %Scopes;
}

######################################################################
sub FindInclusionRanges ($$@) {
	my ($StartAddress, $EndAddress, @ExclusionRanges) = @_;
# Purpose: to calculate and return the DHCP inclusion ranges out of
#          data provided by the NT4 DHCP server
# input:	$StartAddress:
#        $EndAddress:	
#        @ExclusionRanges
# output: none
# return: An arry of IP address pair representing the inclusion ranges
#         in the native registry format.
#

	my ($SA, $EA, @ER);
	$SA = unpack("L", $StartAddress);
	$EA = unpack("L", $EndAddress);
	@ER = @ExclusionRanges;
	for (my $i=0; $i<=$#ER; $i++) {
		$ER[$i] = unpack ("L", $ER[$i]);
	}

	my @InclusionRanges;


	$InclusionRanges[0] = $SA;
	$InclusionRanges[1] = $EA;

	for (my $i=0; $i<=$#ER; $i+=2) {
		if ($ER[$i] == $InclusionRanges[$#InclusionRanges-1]) {
			$InclusionRanges[$#InclusionRanges-1] = $ER[$i+1] + 1;
		}
		if ($ER[$i] > $InclusionRanges[$#InclusionRanges-1]) {
			$InclusionRanges[$#InclusionRanges] = $ER[$i]-1;
		}
		if (($ER[$i+1] > $InclusionRanges[$#InclusionRanges]) && 
		    ($ER[$i+1] != $EA)) {
			$InclusionRanges[$#InclusionRanges+1] = $ER[$i+1] + 1;
			$InclusionRanges[$#InclusionRanges+1] = $EA;
		}
		if ($InclusionRanges[$#InclusionRanges] < $InclusionRanges[$#InclusionRanges-1]) {
			$#InclusionRanges -= 2;
		}
	}

	for (my $i=0; $i<=$#InclusionRanges; $i++) {
		$InclusionRanges[$i] = pack("L", $InclusionRanges[$i]);
	#	print "Inclusion: ", Registry::ExtractIp($InclusionRanges[$i]), "\n";
	}
	return @InclusionRanges;
}

####################################################################
sub GetSuperScope ($) {
	my ($Server) = @_;
	my (%SuperScopes);
#
# purpose: gets the Superscope list from the given server
# input:
#   $Server:  string of the valid IP address or name of the NT4 server
# ouput: none
# return:
#   %SuperScopes: hash of array subnets with the following data structure
#          $SuperScopes{<SuperscopeName>} => array of sunbets
#
	my (@SuperScopeNames, @SCSubnetList);

	print "Getting Superscope list: ";
	if (!Registry::GetRegSubkeyList ("\\\\$Server\\HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Services\\DHCPServer\\Configuration\\SuperScope", \@SuperScopeNames)) {
		for (my $i=0; $i<=$#SuperScopeNames; $i++) {
			print ".";
			if (!Registry::GetRegSubkeyList ("\\\\$Server\\HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Services\\DHCPServer\\Configuration\\SuperScope\\$SuperScopeNames[$i]", \@SCSubnetList)) {
				$SuperScopes{$SuperScopeNames[$i]} = [@SCSubnetList];
			}
		}
		print "\n";
	}

	return %SuperScopes;
}

####################################################################
sub GetGlobalOptions($) {
	my ($Server) = @_;
	my (%GlobalOptions);
# purpose: to return NT4 server's global scope configuration
# input:
#   $Server: string of the valid IP or name of the NT4 server
# output: none
# return:
#   %GlobalOptions: hash of hash of various data types to be returned of the 
#           following data structure
#     $GlobalOptions{routers} => array of IP address strings
#     $GlobalOptions{dnses} => array of IP address/name string
#     $GlobalOptions{domain} > string
#     $GlobalOptions{broadcast} => string
#     $GlobalOptions{winses} => array of IP addresses/name string
#     $GlobalOptions{winstype} => integer

	my ($RegVal, @temp, @GlobalOptionValues);

	print "Getting Global Options: ";
	if (Registry::GetRegSubkeyList ("\\\\$Server\\HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Services\\DHCPServer\\Configuration\\GlobalOptionValues", \@GlobalOptionValues)) { 
		die "Unable to obtain GlobalOptionValues"; 
	}
	
	for (my $i=0; $i<=$#GlobalOptionValues; $i++) {
		print ".";
		if (Registry::GetRegKeyVal ("\\\\$Server\\HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Services\\DHCPServer\\configuration\\globaloptionvalues\\$GlobalOptionValues[$i]\\optionvalue", \$RegVal)) { 
			die "Unable to retrive global option $GlobalOptionValues[$i]\n";
		}
	
	
		for ($GlobalOptionValues[$i]) {
			/003/ and do {
				@temp=Registry::ExtractOptionIps($RegVal);
				$GlobalOptions{routers} = [@temp];
				last;
			};
			/006/ and do {
				# DNS Servers
				@temp = Registry::ExtractOptionIps($RegVal);
				for (my $d=0; $d<=$#temp; $d++) {
					my ($ipname, $rest) = gethostbyaddr(pack("C4", split(/\./, $temp[$d])), &AF_INET);
					$GlobalOptions{dnses}[$d] = $ipname ? $ipname : $temp[$d];
				}
				last;
			};
			/015/ and do { 
				# Domain Name
				@temp = Registry::ExtractOptionStrings($RegVal);
				$GlobalOptions{domain} = $temp[0];
				last;
			};
			/028/ and do { 
				# broadcast address
				@temp = Registry::ExtractOptionIps($RegVal);
				$GlobalOptions{broadcast} = $temp[0];
				last;
			};
			/044/ and do {
				# WINS Servers
				@temp = Registry::ExtractOptionIps ($RegVal);
				$GlobalOptions{winses} = [@temp];
				for (my $w=0; $w<=$#temp; $w++) {
					my ($ipname, $rest) = gethostbyaddr(pack("C4", split(/\./, $temp[$w])), &AF_INET);
					$GlobalOptions{winses}[$w] = $ipname ? $ipname : $temp[$w];
				}
				last;
			};
			/046/ and do {
				# NETBIOS node type
				@temp = Registry::ExtractOptionHex($RegVal);
				$GlobalOptions{winstype} = hex($temp[0]);
				last;
			};
			die "This program does not recgonize global option \#$GlobalOptionValues[$i] yet!\n"
		}
	}
	print "\n";

	return %GlobalOptions;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -