stateofidleness Posted September 18, 2008 Share Posted September 18, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/124834-new-guy-need-some-help-with-ping-output-and-formatting/ Share on other sites More sharing options...
adam291086 Posted September 18, 2008 Share Posted September 18, 2008 so you have it pinging on address and you just want a loop? Post the code you have so far Quote Link to comment https://forums.phpfreaks.com/topic/124834-new-guy-need-some-help-with-ping-output-and-formatting/#findComment-644888 Share on other sites More sharing options...
stateofidleness Posted September 18, 2008 Author Share Posted September 18, 2008 $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] Quote Link to comment https://forums.phpfreaks.com/topic/124834-new-guy-need-some-help-with-ping-output-and-formatting/#findComment-644985 Share on other sites More sharing options...
genericnumber1 Posted September 18, 2008 Share Posted September 18, 2008 http://pear.php.net/package/Net_Ping Will save you a LOT of hastle. Quote Link to comment https://forums.phpfreaks.com/topic/124834-new-guy-need-some-help-with-ping-output-and-formatting/#findComment-644988 Share on other sites More sharing options...
stateofidleness Posted September 18, 2008 Author Share Posted September 18, 2008 I've ran across that PEAR thing a few times. Haven't played with it at all as I had already found a "solution." Until then, thoughts on my current setup? Quote Link to comment https://forums.phpfreaks.com/topic/124834-new-guy-need-some-help-with-ping-output-and-formatting/#findComment-645131 Share on other sites More sharing options...
DarkWater Posted September 18, 2008 Share Posted September 18, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/124834-new-guy-need-some-help-with-ping-output-and-formatting/#findComment-645135 Share on other sites More sharing options...
stateofidleness Posted September 18, 2008 Author Share Posted September 18, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/124834-new-guy-need-some-help-with-ping-output-and-formatting/#findComment-645140 Share on other sites More sharing options...
Zane Posted September 18, 2008 Share Posted September 18, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/124834-new-guy-need-some-help-with-ping-output-and-formatting/#findComment-645142 Share on other sites More sharing options...
DarkWater Posted September 18, 2008 Share Posted September 18, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/124834-new-guy-need-some-help-with-ping-output-and-formatting/#findComment-645143 Share on other sites More sharing options...
Stooney Posted September 18, 2008 Share Posted September 18, 2008 I disagree, I think AJAX would be well worth it as it would solve the issue of updating the output on a per loop basis, and it could eliminate the need to ever need to refresh the page. Quote Link to comment https://forums.phpfreaks.com/topic/124834-new-guy-need-some-help-with-ping-output-and-formatting/#findComment-645144 Share on other sites More sharing options...
stateofidleness Posted September 18, 2008 Author Share Posted September 18, 2008 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! Quote Link to comment https://forums.phpfreaks.com/topic/124834-new-guy-need-some-help-with-ping-output-and-formatting/#findComment-645148 Share on other sites More sharing options...
Zane Posted September 18, 2008 Share Posted September 18, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/124834-new-guy-need-some-help-with-ping-output-and-formatting/#findComment-645168 Share on other sites More sharing options...
stateofidleness Posted September 18, 2008 Author Share Posted September 18, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/124834-new-guy-need-some-help-with-ping-output-and-formatting/#findComment-645225 Share on other sites More sharing options...
stateofidleness Posted September 19, 2008 Author Share Posted September 19, 2008 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... Quote Link to comment https://forums.phpfreaks.com/topic/124834-new-guy-need-some-help-with-ping-output-and-formatting/#findComment-645274 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.