Jump to content

Need Help With Code


jefffogel1974

Recommended Posts

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!!!

 

ping.png

 

Link to comment
Share on other sites

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!!!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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);
?>

Link to comment
Share on other sites

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>";
}
Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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 by dalecosp
Link to comment
Share on other sites

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.