jefffogel1974 Posted November 4, 2013 Share Posted November 4, 2013 Keep in mind I am a PHP/MySQL noob, but my boss has tasked me with a project knowing I don't know coding to well. Here are the details. We currently have a building that has 20 some Cisco Wireless Access Points and all have an IP address assigned to them. I need to create a webpage that will pull the data I have from MySQL and display it in a nice HTML table layout. This is not the problem and I have completed this part thanks to a script I found online. The problem I am running into is my boss would like the script to grab the IP address of each access point from the DB and ping it and if it is down he would like it to display the words OFFLINE next to that AP in the HTML table. If the AP is up he would like to have it display ONLINE in the table next to the AP. If anyone can lend some assistance to a total noob I would greatly be in your debt. Thank you for everyone that assists!!! Quote Link to comment https://forums.phpfreaks.com/topic/283585-need-help-with-code/ Share on other sites More sharing options...
dalecosp Posted November 4, 2013 Share Posted November 4, 2013 system("/path/to/ping $ipaddress",$foo); if ($foo) { echo "IP $address is offline!";} UNIX-based, anyway. Keep in mind that a system call that's successful returns zero Quote Link to comment https://forums.phpfreaks.com/topic/283585-need-help-with-code/#findComment-1456889 Share on other sites More sharing options...
AbraCadaver Posted November 4, 2013 Share Posted November 4, 2013 Assuming everything that you've shown is working independently, just add your ping.php code into the while loop in list.php and change the 8.8.8.8 to $row['IP_Address']. Quote Link to comment https://forums.phpfreaks.com/topic/283585-need-help-with-code/#findComment-1456890 Share on other sites More sharing options...
dalecosp Posted November 4, 2013 Share Posted November 4, 2013 Incidentally, I now notice you have similar code in your left editor of the screenshot. What is the problem, exactly? Quote Link to comment https://forums.phpfreaks.com/topic/283585-need-help-with-code/#findComment-1456891 Share on other sites More sharing options...
jefffogel1974 Posted November 4, 2013 Author Share Posted November 4, 2013 Thank you guys for the suggestions. Dale the left editor is just another script I found that can ping an IP address but I can only get it to work if I statically assign the IP address, I actually want the script to loop through what is displayed from my right side editor and display Offline/Online status. Abra, I really like your idea, but I am such a noob with PHP/MySql that I am unsure how to put the ping.php code into a while loop and was wondering if you could show me this? Thanks again you guys for your help!!! Quote Link to comment https://forums.phpfreaks.com/topic/283585-need-help-with-code/#findComment-1456894 Share on other sites More sharing options...
AbraCadaver Posted November 4, 2013 Share Posted November 4, 2013 Thank you guys for the suggestions. Dale the left editor is just another script I found that can ping an IP address but I can only get it to work if I statically assign the IP address, I actually want the script to loop through what is displayed from my right side editor and display Offline/Online status. Abra, I really like your idea, but I am such a noob with PHP/MySql that I am unsure how to put the ping.php code into a while loop and was wondering if you could show me this? Thanks again you guys for your help!!! Just paste the ping code in the existing while loop and format the echo how you want inside a td /td or whatever. Quote Link to comment https://forums.phpfreaks.com/topic/283585-need-help-with-code/#findComment-1456904 Share on other sites More sharing options...
jefffogel1974 Posted November 4, 2013 Author Share Posted November 4, 2013 I am either not pasting the correct code into the right area or it just doesn't work because now I get a blank page when I try and view the webpage. This is the whole code of what I did below. <?php $con=mysqli_connect("192.168.1.158","jfogel","Slave123","aps"); // Check connection if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } $result = mysqli_query($con,"SELECT * FROM aps"); echo "<table border='1'> <tr> <th>ID</th> <th>Site</th> <th>AP Name</th> <th>IP Address</th> <th>MAC Address</th> <th>AP Location</th> <th>Status</th> </tr>"; while($row = mysqli_fetch_array($result)) $str = exec("ping -n 1 -w 2000 $row['IP_Address']", $input, $result); if ($result == 0){ { echo "<tr>"; echo "<td>" . $row['ID'] . "</td>"; echo "<td>" . $row['Site'] . "</td>"; echo "<td>" . $row['AP_Name'] . "</td>"; echo "<td>" . $row['IP_Address'] . "</td>"; echo "<td>" . $row['MAC_Address'] . "</td>"; echo "<td>" . $row['AP_Location'] . "</td>"; echo "<td>" . $row['echo "Online"; }else{ echo "Offline"; } '] . "</td>"; echo "</tr>"; } echo "</table>"; mysqli_close($con); ?> Quote Link to comment https://forums.phpfreaks.com/topic/283585-need-help-with-code/#findComment-1456905 Share on other sites More sharing options...
Ch0cu3r Posted November 4, 2013 Share Posted November 4, 2013 Code for the while loop should be while($row = mysqli_fetch_array($result)) { echo "<tr>"; echo "<td>" . $row['ID'] . "</td>"; echo "<td>" . $row['Site'] . "</td>"; echo "<td>" . $row['AP_Name'] . "</td>"; echo "<td>" . $row['IP_Address'] . "</td>"; echo "<td>" . $row['MAC_Address'] . "</td>"; echo "<td>" . $row['AP_Location'] . "</td>"; echo "<td>"; $str = exec("ping -n 1 -w 2000 {$row['IP_Address']}", $input, $result); if ($result == 0){ echo "Online"; }else{ echo "Offline"; } echo "</td>"; echo "</tr>"; } Quote Link to comment https://forums.phpfreaks.com/topic/283585-need-help-with-code/#findComment-1456906 Share on other sites More sharing options...
Barand Posted November 4, 2013 Share Posted November 4, 2013 If that is your password above, I would change it immediately now you have published it Quote Link to comment https://forums.phpfreaks.com/topic/283585-need-help-with-code/#findComment-1456909 Share on other sites More sharing options...
jefffogel1974 Posted November 4, 2013 Author Share Posted November 4, 2013 Already did after I realized my mistake, thank you!! Quote Link to comment https://forums.phpfreaks.com/topic/283585-need-help-with-code/#findComment-1456913 Share on other sites More sharing options...
jefffogel1974 Posted November 4, 2013 Author Share Posted November 4, 2013 Why can I not edit my post to remove this? Quote Link to comment https://forums.phpfreaks.com/topic/283585-need-help-with-code/#findComment-1456914 Share on other sites More sharing options...
dalecosp Posted November 4, 2013 Share Posted November 4, 2013 Not sure; if I remember right, minimum post count, or it's a time-out and you could've if you'd done it quick enough ... but it appears that your posts are two minutes apart so I'm guess it's the post count... Quote Link to comment https://forums.phpfreaks.com/topic/283585-need-help-with-code/#findComment-1456934 Share on other sites More sharing options...
dalecosp Posted November 4, 2013 Share Posted November 4, 2013 (edited) Hum diddy ... (sorry, bit by another bug apparently ...) Edited November 4, 2013 by dalecosp Quote Link to comment https://forums.phpfreaks.com/topic/283585-need-help-with-code/#findComment-1456935 Share on other sites More sharing options...
jefffogel1974 Posted November 6, 2013 Author Share Posted November 6, 2013 Ch0cu3r that worked very good thank you, but it only displays 1 table row, how would I loop it through to ping each AP in the database? Quote Link to comment https://forums.phpfreaks.com/topic/283585-need-help-with-code/#findComment-1457281 Share on other sites More sharing options...
dalecosp Posted November 7, 2013 Share Posted November 7, 2013 That should display all of them.Can you put something like: $result = mysqli_query($con,"SELECT * FROM aps"); //doublecheck our result list size echo "Got ".$result->num_rows." access points from DB."; Quote Link to comment https://forums.phpfreaks.com/topic/283585-need-help-with-code/#findComment-1457442 Share on other sites More sharing options...
Ch0cu3r Posted November 8, 2013 Share Posted November 8, 2013 Ch0cu3r that worked very good thank you, but it only displays 1 table row, how would I loop it through to ping each AP in the database? Not sure. I have only add ed your ping code to the while loop. Maybe the exec() is stopping the while loop from displaying the APs. If you remove the ping code $str = exec("ping -n 1 -w 2000 {$row['IP_Address']}", $input, $result); if ($result == 0){ echo "Online"; }else{ echo "Offline"; } Does it now display all the APs? Quote Link to comment https://forums.phpfreaks.com/topic/283585-need-help-with-code/#findComment-1457510 Share on other sites More sharing options...
jefffogel1974 Posted November 8, 2013 Author Share Posted November 8, 2013 Hi Dale, yes it displays the text "Got 219 access points from DB." Ch0cu3r yes if I remove the ping command it does list all access points in a nice table format, so seems like something in the ping command is stopping it from displaying all AP's?? Quote Link to comment https://forums.phpfreaks.com/topic/283585-need-help-with-code/#findComment-1457593 Share on other sites More sharing options...
dalecosp Posted November 11, 2013 Share Posted November 11, 2013 (edited) Argh! We've got an issue here. Dunno why I didn't see this before ... are you using "$result" for the DB resource, and "$result" for the result of the ping command as well?Try changing the one in the ping command to "$pingresult" or something like that, and see if we have a better, um, "result" ... Edited November 11, 2013 by dalecosp Quote Link to comment https://forums.phpfreaks.com/topic/283585-need-help-with-code/#findComment-1457882 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.