Passing Data to Fping

119 views
Skip to first unread message

Magua

unread,
Jul 2, 2009, 1:52:52 PM7/2/09
to Professional PHP Developers
I am attempting to write a php fping script and I am having trouble
passsing the data to the executable. As you know fping pings in
parallel making it very fast making it very desirable.

The following works the command prompt #

#fping -a < ip_table.txt

The text file contains multiple IP addresses separated by carriage
returns.

What i am trying to do is the same thing in a php script.
So far i can get the following to work.

<?
$host = "66.206.114.113";
$fping = exec("ping $host -ac1 -w2 | awk '/time/ {print $8}' | head -1
| awk -F '=' '{print $2}'");
?>

However this does not...
<?
$myFile = "ip_hosts.txt";
$fh = fopen($myFile, 'r');
$host = fread($fh, filesize($myFile));
fclose($fh);

function do_ping($host, $fping, $p_result) {
echo $p_result . "<br>";
$fping = exec("ping $host -ac1 -w2 | awk '/time/ {print $8}' | head -1
| awk -F '=' '{print $2}'");

?>

So basiscally how do you take data from text file and pass it to an
executable like fping?

PatiDure

unread,
Jul 3, 2009, 4:17:37 AM7/3/09
to Professional PHP Developers
One way to do it:

Add data to file, one host per line. Then read that data and extract
the contents to array, one line per element.
You can use eg. explode function for that, like $rows = explode("\n",
$dataFromFile);

Then iterate that in foreach loop and call the function for every row

eg.

foreach($rows as $id=>$host)
{
do_ping($host, $fping, $p_result) ;
}

--
Henri Hovi
http://www.patidure.com

Robert Gonzalez

unread,
Jul 3, 2009, 6:38:32 PM7/3/09
to Professi...@googlegroups.com
Why not just use file()?
--
Robert A. Gonzalez
Reply all
Reply to author
Forward
0 new messages