My interpretation, from the way the question is phrased, is that you are looking for the DC that logged you in.

That info is in the environment variable "LOGONSERVER". in perl, you can get the info by querying

$ENV{LOGONSERVER}
In case you are looking for more generic info on how to find all DC's for a domain, here is my code for that:
use strict; use Net::DNS; use Win32::OLE; # Specify the domain name on the command line # or use the default. my $domain = $ARGV[0] || 'YourDomainName.com'; print "====Querying for Domain Controllers in $domain====\n"; my %dcinfo = DNS_SRV_Query($domain, "_ldap._tcp.dc._msdcs"); while (my ($n,$IP) = each %dcinfo){ print " DC: $n [$IP]\n"; } my $forest = $ARGV[0] || 'YourForestName.com'; print "====Querying for Global Catalog servers in $forest====\n"; %dcinfo = DNS_SRV_Query($forest, "_ldap._tcp.gc._msdcs"); while (my ($n,$IP) = each %dcinfo){ print " GC: $n [$IP]\n"; } print "====Querying FSMO, Infrastrasturcture master servers in $domain +====\n"; FSMO_Query($domain); ########################## sub DNS_SRV_Query{ my ($name,$DNSPrefix) = @_; my %results; my $DNS = Net::DNS::Resolver->new(); my $query = $DNS->search("$DNSPrefix.$domain", "SRV") or die "DNS SRV query ($DNSPrefix.$domain) failed: ", $DNS->errors +tring, "\n"; foreach my $rr ($query->answer) { next unless $rr->type eq "SRV"; my $query2 = $DNS->search($rr->target, "A"); if ($query2) { foreach my $rr2 ($query2->answer) { next unless $rr2->type eq "A"; $results{$rr->target} .= $rr2->address . " "; } } } return %results; } ###################################### sub FSMO_Query{ my $strDomain = shift or die "No domain name specified"; # e.g. eme +a.rallencorp.com $Win32::OLE::Warn = 3; my $objRootDSE = Win32::OLE->GetObject("LDAP://$strDomain/RootDSE"); my $strDomainDN = $objRootDSE->Get("defaultNamingContext"); my $strSchemaDN = $objRootDSE->Get("schemaNamingContext"); my $strConfigDN = $objRootDSE->Get("configurationNamingContext"); PrintMasterName($strDomainDN,"PDC Emulator"); PrintMasterName("cn=RID Manager\$,cn=system","RID Master", $strDomai +nDN); PrintMasterName($strSchemaDN,"Schema Master"); PrintMasterName("cn=Partitions","Domain Naming Master",$strConfigDN) +; PrintMasterName("cn=Infrastructure","Infrastructure Master",$strDoma +inDN); # PDC Emulator my $objPDCFsmo = Win32::OLE->GetObject("LDAP://" . $strDomainDN); print "PDC Emulator: ", $objPDCFsmo->fsmoroleowner,"\n"; my $PDC = $objPDCFsmo->fsmoroleowner; print "I think the PDC name is :" . ExtractName($PDC,$strDomain) . " +\n"; # RID Master my $objRIDFsmo = Win32::OLE->GetObject("LDAP://cn=RID Manager\$,cn=s +ystem,$strDomainDN"); print "RID Master: ", $objRIDFsmo->fsmoroleowner, "\n"; print "I think the RID Master is :" . ExtractName($objRIDFsmo->fsmor +oleowner,$strDomain) . "\n"; # Schema Master my $objSchemaFsmo = Win32::OLE->GetObject("LDAP://" . $strSchemaDN); print "Schema Master: ", $objSchemaFsmo->fsmoroleowner,"\n"; print "I think the Schema Master is :" . ExtractName($objSchemaFsmo- +>fsmoroleowner,$strDomain) . "\n"; # Infrastructure Master my $objInfraFsmo = Win32::OLE->GetObject("LDAP://cn=Infrastructure," + . $strDomainDN); print "Infrastructure Master: ", $objInfraFsmo->fsmoroleowner,"\n"; # Domain Naming Master my $objDNFsmo = Win32::OLE->GetObject("LDAP://cn=Partitions," . $str +ConfigDN); print "Domain Naming Master: ", $objDNFsmo->fsmoroleowner,"\n"; print "I think the Domain Naming Master is :" . ExtractName($objDNFs +mo->fsmoroleowner,$strDomain) . "\n"; } ##################################3 sub PrintMasterName{ my ($FetchDN, $Type, $SourceDN) = @_; my $obj = Win32::OLE->GetObject("LDAP://$FetchDN" . ($SourceDN ? ",$SourceDN" : "") ); # print "==$Type DN: ", $obj->fsmoroleowner, "\n"; print "==I think the $Type is :" . ExtractName($obj->fsmoroleowner,$ +domain) . "\n"; } ##################################3 sub ExtractName{ my $str=shift; my @dom = split /\./, shift; STRINGPIECE: for (split /,/, $str){ m/Settings$/i and next; m/Servers$/i and next; m/Sites$/i and next; m/Configuration$/i and next; for my $domain_piece(@dom){ m/$domain_piece/ and next STRINGPIECE; } m/CN=(\w+)/; return $1; # Passed all tests ! } }

    Earth first! (We'll rob the other planets later)


In reply to Re: Get Domain Controller by NetWallah
in thread Get Domain Controller by slloyd

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.