Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to ping ip addresses in php and give results [duplicate]

Possible Duplicate:
Pinging an IP address using PHP and echoing the result

How do you ping an ip addresses in php. and give the the results as if you are on cmd program in windows

<?php

  system(‘ping -c 192.168.0.104’); // Ping IP address.<br>

   echo “pinged”;<br>

?>
like image 897
Mervyn Avatar asked Nov 08 '12 06:11

Mervyn


People also ask

How do you ping a repeated IP address?

Use the command "ping 192.168. 1.101 -t" to initiate a continuous ping. Again, replace the IP address with one specific to your device as needed. The -t can be placed before or after the IP address.

How do I get multiple ping results in a text file?

Let's call it "servers. txt" and save it (as you going to ping server names so make sure name resolution is happening). Next, open Command Prompt and navigate to the folder where you just created the file in which you listed the name of servers. This will attempt to ping every system in the list and return the result.

Can you ping multiple IP addresses?

While the ping command is used to ping a single host device to identify its existence, ping sweep helps to ping multiple IP addresses simultaneously. It's a basic network scanning technique used to determine the range of active and inactive IP addresses available on the network.


2 Answers

Try this

$host="192.168.0.104";

exec("ping -c 4 " . $host, $output, $result);

print_r($output);

if ($result == 0)

echo "Ping successful!";

else

echo "Ping unsuccessful!";

Note: This is dependant on the OS you are running. Windows will default to only 4 pings while Linux will ping forever.

To ping twice in Windows, use "ping -n 2 host"

To ping twice in Linux, use "ping -c 2 host"

like image 144
Hkachhia Avatar answered Oct 07 '22 16:10

Hkachhia


$ip =   "127.0.0.1";
exec("ping -n 3 $ip", $output, $status);
print_r($output);

output looks like below

Array
(
    [0] => 
    [1] => Pinging 127.0.0.1 with 32 bytes of data:
    [2] => Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
    [3] => Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
    [4] => Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
    [5] => 
    [6] => Ping statistics for 127.0.0.1:
    [7] =>     Packets: Sent = 3, Received = 3, Lost = 0 (0% loss),
    [8] => Approximate round trip times in milli-seconds:
    [9] =>     Minimum = 0ms, Maximum = 0ms, Average = 0ms
)
like image 34
arun Avatar answered Oct 07 '22 15:10

arun