how to fetch domain names from a tracert/traceroute result in php without using 'http://' and 'www.' as needle?

StackOverflow https://stackoverflow.com/questions/8582937

  •  23-03-2021
  •  | 
  •  

Question

My motive is to get the hosting provider of a domain name using php. When tracert/traceroute to any domain name (yahoo.com), it gives:

    traceroute to 72.30.2.43 (72.30.2.43), 30 hops max, 40 byte packets
     1  c1.25.78ae.static.theplanet.com (174.120.37.193)  0.648 ms  0.630 ms  0.647 ms
     2  te6-2.dsr02.dllstx2.networklayer.com (70.87.254.237)  0.365 ms  0.430 ms  0.462 ms
     3  te7-4.dsr02.dllstx3.networklayer.com (70.87.253.121)  0.562 ms te4-3.dsr02.dllstx3.networklayer.com (70.87.255.129)  0.910 ms te4-4.dsr02.dllstx3.networklayer.com (70.87.255.133)  0.664 ms
     4  ae17.bbr01.eq01.dal03.networklayer.com (173.192.18.226)  0.403 ms ae17.bbr02.eq01.dal03.networklayer.com (173.192.18.230)  0.380 ms  0.405 ms
     5  ae1.bbr01.cf01.den01.networklayer.com (173.192.18.139)  14.864 ms  14.723 ms  14.770 ms
     6  ae1.bbr01.cf01.den01.networklayer.com (173.192.18.139)  14.787 ms 198.32.216.25 (198.32.216.25)  14.882 ms  14.946 ms
     7  198.32.216.25 (198.32.216.25)  14.863 ms ae-7.pat1.pao.yahoo.com (216.115.101.128)  38.403 ms ae-7.pat1.sjc.yahoo.com (216.115.101.149)  41.250 ms
     8  ae-1-d420.msr1.sk1.yahoo.com (216.115.106.161)  38.930 ms ae-1-d400.msr1.sk1.yahoo.com (216.115.106.153)  41.643 ms ae-0-d210.msr2.sk1.yahoo.com (216.115.106.133)  38.617 ms
     9  te-8-1.bas-k1.sk1.yahoo.com (68.180.160.9)  41.478 ms te-9-1.bas-k2.sk1.yahoo.com (68.180.160.15)  39.368 ms ae-0-d230.msr2.sk1.yahoo.com (216.115.106.141)  42.650 ms
    10  * * te-8-1.bas-k2.sk1.yahoo.com (68.180.160.11)  41.787 ms
    11  * * *
    12  * * *
    13  * * *
    14  * * *
    15  * * *
    16  * * *
    17  * * *
    18  * * *
    19  * * *
    20  * * *
    21  * * *
    22  * * *
    23  * * *
    24  * * *
    25  * * *
    26  * * *
    27  * * *
    28  * * *
    29  * * *
    30  * * *

I want to get to the last valid domain name, which is (in this case):

`10  * * te-8-1.bas-k2.sk1.yahoo.com (68.180.160.11)  41.787 ms`

I can get to this easily by using this code (stristr()):

    $a = explode("\n",$str);
    foreach($a as $v){
        if(!stristr($v,'* * *')){
            echo $v.'
'; } }

I need this result:

    yahoo.com

it should be from the # 10th (10 * * te-8-1.bas-k2.sk1.yahoo.com (68.180.160.11) 41.787 ms)

Does anyone has a solution to this? Or if someone has better solution to my basic objective here.

Would really appreciate any help.

Thank you!

Was it helpful?

Solution

//Cut the excess off.

$starPos = strpos($str,'* * *');
$subSet = substr($str,0,$starPos);
$a = explode("\n",$subSet);
$last = $a[count($a)-2] ;

//Preg match the domain
echo "RESULT: ";
if (preg_match('/\.([^\.]*?\.[^\.]*?)\s\(\d/i', $last, $regs)) {
    echo $result = $regs[1];
} else {
    echo $result = "";
}

OTHER TIPS

While just using |cut or str_split('/\s+/') might work for splitting and manual extraction, you can also use a regex with enough specificity to extract all host names at once:

preg_match_all('/(?<=\s)([\w-]+\.){2,}[a-z]+(?=\s\()/', $tracert, $m);
print_r($m[0]);

This depends on a letters-only TLD, and minimum two more sub.domain. prefixes. A {1,} might however suffice.

You have to perform two steps: Step 1 is to extract the last hostname from the array:

$a = explode("\n",$str);
$hosts = preg_grep('/\s[^(]+\s\(/', $a);
$lastHost = $hosts[count($hosts)-1];
if (preg_match('/\s([^( ]+)\s\(/i', $lastHost, $result)) {
    $hostname = $result[1];
}
// this will give you "te-8-1.bas-k2.sk1.yahoo.com"

Step 2: Identify the "top-level" hostname.

This is a rather tedious task, since there are new domains popping up all over the world constantly. Thanks to the internet, you are not alone with the problem: The Mozilla Foundation provides an up-to-date list of top level domains including their first level subdomain (like .co.uk or .ws.ru):

The list can be downloaded here. You don't have to implement this on your own, though, but you can download a "ready-to-run-module" here. Download it, add it to your project and run it:

require_once 'effectiveTLDs.inc.php';
require_once 'regDomain.inc.php';
$realHostname = getRegisteredDomain($hostname);
// gives you 'yahoo.com'

Make sure to update these files on a regular base and you should be all set up.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top