Hi guys, I have a variable generated by a shell_exec(); command in PHP. The string looks like this:

Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.7.0     0.0.0.0         255.255.255.0   U     0      0        0 eth1
192.168.8.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0
0.0.0.0         192.168.1.60    0.0.0.0         UG    0      0        0 eth0

What I need is a way using PHP to get the number 192.168.1.60 everytime. The trick is that the length of the list may change but there will always only be one entry beginning with 0.0.0.0.

Any ideas?

Thanks

    Easiest way I can think of would be to use [man]preg_match/man with a pattern like:

    /^0\.0\.0\.0\s+([0-9.]+)\s/s

      Thanks for your reply. I am not getting any results from this code:

      <?php
      $command = shell_exec('route -n');
      preg_match('/^0\.0\.0\.0\s+([0-9.]+)\s/s', $command, $matches);
      echo($matches[1]);
      ?>
      

      Any ideas?

      Thanks a million

        I guess you should remove '' in the begining.

        Like this

        <?php
        
        $command = shell_exec('route -n');
        preg_match('/0\.0\.0\.0\s+([0-9.]+)\s/s', $command, $matches);
        echo($matches[1]);
        
        ?> 

          Thanks for your help. I run the code and it doesnt produce the expcted results. If I do a print_r it shows:

          Array
          (
              [0] => 0.0.0.0         255.255.255.0 
              [1] => 255.255.255.0
          )
          

          What I needed was 192.168.1.60.

          Thanks

            The '' should be in the pattern - it specifies that you want the beginning of the line (which you do).

            My problem was that I used the wrong modifier. Try this instead:

            /^0\.0\.0\.0\s+([0-9.]+)/m

            EDIT: To clarify, this page explains what the 'm' modifier does:

            /m enables "multi-line mode". In this mode, the caret and dollar match before and after newlines in the subject string.

              Write a Reply...