use Win32::Lanman; use strict; #============================================================================== my $Program = "EnumerateNT.pl"; my $Version = "1.00"; my $Author = "Howard A. Bullock"; my $Created = "10-Sep-1999"; my $LastModified = "11-Sep-1999"; #============================================================================== # # This program enumerates visible NT servers by domain and role. # # Syntax: 'perl EnumerateNT.pl [Domain_name] [Domain_name] ...' # # Without the Domain_name parameter, all known domains are processed. # # The results are displayed to STDOUT and to the file 'EnumerateNT.log' in the # current directory. # # ================================================================================== print "\n------------------------------------------------------\n"; print "Program Name: $Program\n"; print " Version: $Version\n"; print " Author: $Author\n"; print " Created: $Created Last Modified: $LastModified\n\n"; if ($#ARGV <0) { print "\nReporting servers and roles for ALL domains.\n"; } else { print "\nReporting servers and roles for domains: @ARGV\n"; } print "------------------------------------------------------\n\n"; my ($a, $cnt, $i, $keys, $domain, $server); my @info = (); my %machines = {}; my $err =0; open(LOG, ">EnumerateNT.log") || die "Can not open log file in the current directory!\n"; if ($#ARGV < 0) { $domain = "amp01"; if(!Win32::Lanman::NetServerEnum("", $domain, SV_TYPE_DOMAIN_ENUM, \@info)) { $a = "$domain Error: " . Win32::Lanman::GetLastError(); map {print $_; print LOG $_;} "$a\n"; exit 1; } foreach $domain (@info) { ($cnt, %machines) = &EnumDomain(${$domain}{'name'}); $i++; $a += $cnt; if ($machines{${$domain}{'name'}}{'PDC'} !~ s/\\\\//) {$err++;} map {print $_; print LOG $_;} "${$domain}{'name'}\tPDC\t$machines{${$domain}{'name'}}{'PDC'}\n"; if ($machines{${$domain}{'name'}}{'BDC'} =~ /Error/i) { map {print $_; print LOG $_;} "${$domain}{'name'}\tBDC\t$machines{${$domain}{'name'}}{'BDC'}\n"; } else { foreach $server (sort keys %{$machines{${$domain}{'name'}}{'BDC'}}) { map {print $_; print LOG $_;} "${$domain}{'name'}\tBDC\t$server\n"; } } if ($machines{${$domain}{'name'}}{'SRV'} =~ /Error/i) { map {print $_; print LOG $_;} "${$domain}{'name'}\tSRV\t$machines{${$domain}{'name'}}{'SRV'}\n"; } else { foreach $server (sort keys %{$machines{${$domain}{'name'}}{'SRV'}}) { map {print $_; print LOG $_;} "${$domain}{'name'}\tSRV\t$server\n"; } } print "\n"; } } else { foreach $domain (@ARGV) { ($cnt, %machines) = &EnumDomain($domain); $i++; $a += $cnt; if ($machines{$domain}{'PDC'} !~ s/\\\\//) { $err++;} map {print $_; print LOG $_;} "$domain\tPDC\t$machines{$domain}{'PDC'}\n"; if ($machines{$domain}{'BDC'} =~ /Error/i) { map {print $_; print LOG $_;} "$domain\tBDC\t$machines{$domain}{'BDC'}\n"; } else { foreach $server (sort keys %{$machines{$domain}{'BDC'}}) { map {print $_; print LOG $_;} "$domain\tBDC\t$server\n"; } } if ($machines{$domain}{'SRV'} =~ /Error/i) { map {print $_; print LOG $_;} "$domain\tSRV\t$machines{$domain}{'SRV'}\n"; } else { foreach $server (sort keys %{$machines{$domain}{'SRV'}}) { map {print $_; print LOG $_;} "$domain\tSRV\t$server\n"; } } print "\n"; print LOG "\n"; } } print "Number of domains: $i\n"; print "Number of servers: $a\n"; print "Number of domain errors: $err\n"; print LOG "Number of domains: $i\n"; print LOG "Number of servers: $a\n"; print LOG "Number of domain errors: $err\n"; close LOG; sub EnumDomain { my $domain = shift; my @info = (); my %machines = {}; my $i = 0; my $pdcname; # Get PDC for domain. if(!Win32::Lanman::NetGetDCName("", $domain, \$pdcname)) { if(!Win32::Lanman::NetGetDCName("\\\\ambdc005", $domain, \$pdcname)) { $machines{$domain}{'PDC'} = "Error: " . Win32::Lanman::GetLastError(); return ($i, %machines); } } $machines{$domain}{'PDC'} = $pdcname; $i++; # Get BDCs if(!Win32::Lanman::NetServerEnum($pdcname, $domain, SV_TYPE_DOMAIN_BAKCTRL, \@info)) { $machines{$domain}{'BDC'} = "Error: " . Win32::Lanman::GetLastError(); } foreach $server (@info) { $i++; $machines{$domain}{'BDC'}{${$server}{'name'}} = 1; } # Get Servers if(!Win32::Lanman::NetServerEnum($pdcname, $domain, SV_TYPE_SERVER_NT, \@info)) { $machines{$domain}{'SRV'} = "Error: " . Win32::Lanman::GetLastError(); } foreach $server (@info) { $i++; $machines{$domain}{'SRV'}{${$server}{'name'}} = 1; } return ($i, %machines); }