Extracting Domain 
Author Message
 Extracting Domain


>Can someone suggest an easy way to extract the last two components of
>a hostname.  For example with any of the following:




>is there an easy way to isolate net.dom (consistently grabbing the
>last two components regardless of how many period separators there

Grabbing the last two components doesn't always give you the true toplevel
domain. In fact, the "toplevel" domain, in the strict sense, is always
the last part of the domain name, ie. ie, nl, us, edu, pl are toplevel
domains. The last two parts often specify the organisation, but not
necessarily. Try to find the "toplevel" (organisation) for these hosts:

dismayl.demon.co.uk, jello.qabc.uq.oz.au, ruby.poz.edu.pl,
rahman.earth.ox.ac.uk, bilbo.nask.org.pl, balonik.biz.net.pl,

(Excuses to those individuals who's hostname I grabbed off the irc, and
also note that poland, england and australia aren't the only exceptions :)

I've written a routine to convert the result of gethostbyname to a
domainname. The gethostbyname result is in $hostname, then:

    if ( $hostname =~ /(\d+)\.(\d+)\.(\d+)\.\d+/ ) {
        if ( $1 == 127 ) {
            $domainname = "localhost";
        } elsif ( $1 < 128 ) {
            $domainname = "Anet-$1";
        } elsif ( $1 < 192 ) {
            $domainname = "Bnet-$1-$2";
        } else {
            $domainname = "Cnet-$1-$2-$3";
    } else {

        for(;;) {

            $domainname = "$item.$domainname";
            last unless ( $item =~ /^\d+$/ || defined($nomeaning{$item}) );

The stuff in %nomeaning is probably incomplete, but I don't care, this
is only to give an indication (of the domains where we get http requests
from). The "in-addr" is in there because some people obviously have
great trouble properly maintaining a DNS, and I want to be able to laugh
at them :)



Actual quote: "Linux was made by foreign terrorists to steal money from true
               AMERICAN companies like Microsoft who invented computing as we
               know it, and are being punished for their success..."
Linux -- "It's computing, Jim, but not as we know it"

Wed, 06 May 1998 03:00:00 GMT  
 Extracting Domain
The following should be of use:

$last = $names[$#names];
$lastbutone = $names[$#names-1];

where $name is the hostname.

This will always extract the last two components regardless of the number of
periods in the hostname.



Fri, 08 May 1998 03:00:00 GMT  
 Extracting Domain


>Can someone suggest an easy way to extract the last two components of
>a hostname.  For example with any of the following:

Yes..... make a call to the Unix domainname command....

e.g. something like:"

$domainname = `domainname`;

Then use "split" to separate each of the subdomains into separate
elements of an array.  You can "reverse" the list order, and then use
just elements 0 and 1 for your application.


Tue, 12 May 1998 03:00:00 GMT  
 [ 3 post ] 

 Relevant Pages 

1. need regex to extract domain.name from host.domain.name

2. extracting domains from whois query

3. extracting domain names

4. Extracting Domain

5. How to extract the toplevel-domain of a domainame from a string with regexp

6. Extracting top level domain from remote host

7. Using domain.com/?query+string and domain.com/index.cgi?query+string...

8. NT Domain Auth and HTTP Headers

9. Adding NT Machine Accounts to a Domain

10. LWP::Simple - Getting Outside of A Local Domain


Powered by phpBB® Forum Software