Search code examples
phpregextraceroute

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


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!


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 = "";
    }