Finds out what country an unknown TLD corresponds to.
#!/usr/bin/perl -w # tldlookup.pl -- looks up the country to which a TLD # corresponds. # seems to me there ought to be a *nix utility # to do this, but I couldn't find one easily. use LWP::Simple; use strict; die "usage: tldlookup.pl [tlds] -- where the tlds do not contain perio +ds.\n" unless @ARGV; my %names; # note this URL *could* conceivably change, so keep that in mind # future expansion : get it to recognize OTHER kinds of TLDs, # not just the country codes. That's version .02, I guess my $tld_url = "http://www.iana.org/cctld/cctld-whois.htm"; my $data = get $tld_url; # Oh, my, this *is* flying by the seat of the pants, isn't it? while (my $tld = shift @ARGV) { ( $names{$tld} ) = ($data =~ /(?:\.$tld)[^A-Z]+([^<]+)/); $names{$tld} = "not found" if (!defined($names{$tld}) || length ($tld) < 2); ; } foreach (keys %names) { print ".$_ => $names{$_}\n"; }

Replies are listed 'Best First'.
Re: TLD lookup
by mdillon (Priest) on Nov 23, 2000 at 08:17 UTC
    this code will grab the root.zone file from NSI and give you a list of all the ICANN-sanctioned TLDs.
    #!/usr/bin/perl -lw use LWP::Simple; use strict; use vars qw($ROOT_ZONE); $ROOT_ZONE = 'ftp://rs.internic.net/domain/root.zone'; my $data = get $ROOT_ZONE or die "Failed to fetch root zone\n"; my @lines = split /\r\n?|\n/, $data; my %tlds; for (@lines) { # ignore all but nameserver (NS) records next unless /\s+IN\s+NS\s+/; # grab the first "column" my ($tld) = split ' ', $_, 2; # ignore entries for the root zone next if $tld eq '.'; # strip off the trailing '.' and count the NS record ++$tlds{substr $tld, 0, -1}; } print for sort keys %tlds;
Re: TLD lookup
by dws (Chancellor) on Nov 23, 2000 at 07:18 UTC
    Since the country codes are fixed by an ISO standard, methinks a persistent cache might be appropriate.
      the standard in question is ISO 3166.

      ccTLDs don't correspond directly to the codes in ISO 3166, since a registrar must petition ICANN to add it to the root zone. however, the code basically has to be in ISO 3166 for ICANN to approve it as a ccTLD, so keeping the whole ISO 3166 list around would be a good way to definitively look up any possible ccTLD.

      also, that standard does change a few times every couple of years. for instance, Western Samoa (WS) was recently changed from "Independent State of Western Samoa" to "Independent State of Samoa". more recently, "Occupied Palistinian Territory" (PS) was added (it actually also was subsequently granted a ccTLD which was added to the root.zone within the last few months).

      Oh, yeah, this was totally a quick "hack it up on a Friday afternoon" exercise (well, Wednesday, which in the US this particular week was a Friday) Note the almost complete lack of error handling and research behind it. But it works pretty well, for all that.

      I was thinking of it more as a "look what you can put together in Perl in five minutes" demonstration. One could regard it as something to build on =)

      Philosophy can be made out of anything. Or less -- Jerry A. Fodor