1

please i use this function to run ping command using PHP and i want to create log file for ping command in details.please advice me as you can. my code

<?php $host=array(
    "test6"  => "127.0.0.1",
    "test5" =>"98.139.180.149",
    "test4" =>"192.168.0.60",
    "test3"  =>"172.217.23.78",
    "test2"  =>"192.168.0.62",
    "test1" =>"184.168.221.104",
); 
$port="";
foreach ($host as $key => $value) { //  $output=shell_exec('ping -n 1 '.$value);  
    if (!$socket = @fsockopen(  $value, 80, $errno, $errstr,  1)) {
        echo '   <table border="1" >
            <td>'.$key.'</td>
            <td>'.$value.'</td>
            <td> offline</td>

        </table>';
    } else {
        echo '   <table border="2">
            <td>'.$key.'</td>
            <td>'.$value.'</td>
            <td> online</td>

        </table>';


    fclose($socket); 
    };
};
SamHecquet
  • 1,818
  • 4
  • 19
  • 26
Ahmed Gaber
  • 115
  • 1
  • 10

1 Answers1

0

Assuming you want to write your table to the log file:

<?php $host=array(
    "test6"  => "127.0.0.1",
    "test5" =>"98.139.180.149",
    "test4" =>"192.168.0.60",
    "test3"  =>"172.217.23.78",
    "test2"  =>"192.168.0.62",
    "test1" =>"184.168.221.104",
); 

$res = fopen('/path/to/logfile.log',"a");

foreach ($host as $key => $value) {
    if (!$socket = @fsockopen(  $value, 80, $errno, $errstr,  1)) {
        $table = '   <table border="1" >
            <td>'.$key.'</td>
            <td>'.$value.'</td>
            <td> offline</td>

        </table>';
        fwrite(res, $table . "\n");
    } else {
        $table = '   <table border="2">
            <td>'.$key.'</td>
            <td>'.$value.'</td>
            <td> online</td>

        </table>';
        fwrite(res, $table . "\n");
        fclose($socket);
    }

}
fclose($res);

Edit: changed to take account of @Phillip's comment

fred2
  • 1,015
  • 2
  • 9
  • 29
  • The semicolon isn't wrong, but also neccessary - the fclose instead was correct.- closing an not available source would throw at least an warning – Philipp Nov 10 '17 at 16:46
  • A decade or more of coding PHP and had no idea you could put a semicolon after a }. Still - they're superfluous, even if they don't cause an error. – fred2 Nov 10 '17 at 17:02
  • SamHecquet: first thank you but i know what you post ,but i need to make log file to show mee pachets all data about pinging – Ahmed Gaber Nov 10 '17 at 17:51
  • fred2, Philipp :first thank you but i know what you post ,but i need to make log file to show mee pachets all data about pinging – Ahmed Gaber Nov 10 '17 at 18:01
  • So you want to capture the output of ping and log it? – fred2 Nov 10 '17 at 19:29
  • I would go back to using shell_exec to run ping, and then grab and write the result to a local file. `fsockopen` is not intended to be used as a tool for pinging servers, and doesn't really do the same thing anyway. Take a look at https://stackoverflow.com/questions/8030789/pinging-an-ip-address-using-php-and-echoing-the-result. – fred2 Nov 10 '17 at 20:53
  • Fred: how i can use port number whis shell excute when i ping like ping 192.168.1.1 :8080 plese tell me how can i do it – Ahmed Gaber Nov 11 '17 at 07:46
  • You can't ping ports, because ping doesn't use TCP or IDP. It's more 'primative' if you like. See https://stackoverflow.com/questions/47226953/how-to-create-log-file-for-ping-command-using-php/47227475?noredirect=1#comment81421553_47227475 Note, you can use nmap to get the open or closed status of ports. You can probably use nmap, and log the status from that. It has a lot more options. BUT, nmap has security issues when run as web user, and may need to be run as root (very bad). Look instead at the Net_Nmap Pear package. http://pear.php.net/package/Net_Nmap/. It may be the way to go. – fred2 Nov 11 '17 at 16:05