Jump to content

Recommended Posts

Hey there! First post. Pretty new to PHP programming and here is my dilemma.

I have an array of IP addresses (they are DVR units).

I want to run through a loop pinging each unit and display only whether it is "$address UP" or "$address down".

I have been successful in getting ping to actually work in php using "exec", "system", and "`ping... ".

 

I've gotten the closest to what I want using "exec".

 

Can this be done?

$info[10]["name"]="021 - Deer Park";
$info[10]["address"]="100.100.196.227";

$infocount = 10;
$online = 0;
$offline = 0;

echo '<div id=header><div id=status1><table border="0" align=left>';

for($i=0; $i<=$infocount; $i++){
$ipconnect = "Packets: Sent = 1, Received= 0, Lost = 1 (100% loss)," ;
$currentip = $info[$i]["address"];
exec("ping -n 1 $currentip", $output, $ret_val);
if ( $ret_val == $ipconnect) {
	echo "<tr><td><span class=green><a href=http://" . $info[$i]["address"] . ">" . $info[$i]["name"] . "</a></span></tr></td>";
	$online++;
}else{ 
	echo "<tr><td><span class=red>" . $info[$i]["name"] . "</span></tr></td>"; 
	$offline++;
}	 
} 
echo '</tr></table></div>';
echo "DVRs Online: " . $online . "<br>DVRs Offline: " . $offline . "<br></div>";
?>

 

 

Seems like i have it working now (using the 3 parameters for "exec") but im wondering if i couldnt be doing this more efficiently.

 

The first code snippet is an example of one the array elements. You can see that it goes through the 'for' loop and prints the status as it goes. This is causing a hiccup later on when I want to print the total online and total offline at the TOP of the page... I have no clue how to do this.

 

Im also wondering if an array is the quickest way to do this. If i have to add a dvr later on, it will require retyping all the index numbers so as to keep them in numerical order.

 

here is a screenshot (attached) of my output thus far:

 

 

[attachment deleted by admin]

You shouldn't have to retype any index numbers.  It does it automatically if you do it right.  Look:

 

<?php
$info[] = array('name' => '021 - Deer Park', 'address' => '100.100.196.227');
//You don't need $infocount. 

 

You don't need $infocount because you can just do a foreach loop to iterate through the array.  Read up on it.  Also, would the Deer Park you're talking about happen to be the one in NY?

ahh ok.

im familiar with foreach's and ill give it a try!

 

Deer Park in Texas :)

 

another issue im facing is im trying to have it display each item as it gets tested, instead it is going through the entire loop, then displaying all the stores at one time.

 

my plan is to have the page refresh itself every so often to update the status of the DVRs. What is going to happen is it will run the whole loop again and THEN display it all at once... not really what I want... Id like it to go back through the loop from the beginning once it finishes and then start over without refreshing the page if possible.

 

any ideas?

so you want a CONSTANT loop.....or a live feed type thing

 

most likely the PINGs are going to be so instant that you won't be able to do them .....one at a time per say.

It's already happening one at a time....it's just too fast for you to see

Aww. D:

 

Anyway, you can't do it like that in PHP.  PHP processes itself before sending any HTML output.  You could use Ajax for this, but I doubt it's worth it.  Just set Javascript up to auto-refresh.

well i lied about the "foreach" stuff lol. .guess it's been too long since i've used it..

 

any help?

 

i've also never touched AJAX so wouldn't know where to start.. if it's worth it to learn for my purposes im all for it though!

 

 

Well the question you have to ask yourself now....if you're interested in AJAX..is

 

what do you want to trigger the refresh of the IP checks?  Do you want to click a button to refesh them all...or wait 5 minutes, or hover the mouse over a sexy image of ellen page.  It's all about what you want to happen.

 

unless you specify as trigger of some sort....there's nowhere to go but to push F5 ever so often.

 

EDIT

a cool idea would be trigger it to refresh the IP whenever you hover over the IP itself....or name or whatever

or ellen page...

 

then you can just run you mouse about the screen updating like a madman

OK! i got the foreach working!!

 

im gonna research this ajax business and see if it would be a better route. On my foreach, it prints each element on a new line.. i tried to have multiple columns but not sure how to go about that.. once col1 reaches bottom of screen, then start a new column..

 

ideally i would have it refresh once it is finished with the loop... leaving the previous loop results visible.. and replacing with the current status as it goes through the loop.. if that makes sense

 

where should i start with ajax? i think i started with php because it was easy to setup a webserver (usb drive :)) with it and only language i could find with a ping function.

ok so.... this ajax looks difficult :)

ill research it some more tomorrow.. i isolated my php code to its own page and redid the code on the main page that "calls" the php page... no dice.. heh.. (couldn't be that easy)

 

assuming i did this with ajax... im not sure how i would set up the "testing"... would i break it out of a foreach loop and then do individual calls from the "main" page to the ping.php page?

 

one thing i forgot to add early on was that, if it detects the DVR as online, it outputs a hyperlink to the units IP address (this is all on a work intranet)

if it detects it's down, it just displays the name in red font.

 

it seems that AJAX will allow me to do what im wanting as far as the "update the page as it goes" idea instead of the entire page at once. I just don't know how to do it yet lol...

 

 

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.