This topic is locked
[SOLVED]

 Trying to Ping from within _View.php

8/11/2011 8:48:37 PM
PHPRunner General questions
G
geman220 author

I'm trying to ping from within the _View.php file. I have 2 columns (IP1 , IP2) I want to be able to ping IP1 and IP2 and show CONNECTED or DISCONNECTED based on the results. I'm attempting the following code:



global $data;

if ($data["IP"])

$value = $data["IP"];

print_r ($data["IP"]);
function ping($host) {

exec(sprintf('ping -n 1 %s', escapeshellarg($host)), $res, $rval);

return $rval === 0;

}
$hosts_to_ping = array($data["IP"]);
foreach ($hosts_to_ping as $host);
$up = ping($host);
$value = $data["IP"].", ". $up ? 'Connected' : 'Disconnected';


Note currently I am just testing and trying to ping one of the IPs right now. I'm trying to set this up so that when I check inventory I'll know which items are online and which are offline quickly. This is setup as a "Custom View as:" where I think it belongs. No matter what IP I put into the database it will always report Connected. Which makes me assume somehow it's not passing the variable of the IP on the _view page. Any help would be greatly apprecaited, I've been slamming my head into a wall over this.
Thanks
EDIT: The Print_r is obvioustly useless I was just testing to see if the variable was getting passed at all, which it seems to be.

Admin 8/12/2011

This code doesn't look right. foreach loop doesn't do anything.
I would suggest to check the following:

  1. Test this code outside of PHPRunner to make sure it works.
  2. Make sure $data["IP"] contains the actual IP address

G
geman220 author 8/12/2011



This code doesn't look right. foreach loop doesn't do anything.
I would suggest to check the following:

  1. Test this code outside of PHPRunner to make sure it works.
  2. Make sure $data["IP"] contains the actual IP address




<HTML>

<HEAD>

<TITLE>CS3 Network Status</TITLE>

</HEAD>

<BODY>

<?php

function ping($host) {

exec(sprintf('ping -n 1 %s', escapeshellarg($host)), $res, $rval);

return $rval === 0;

}
$hosts_to_ping = array('192.168.1.1', '192.168.1.2');

?>
<table>

<?php foreach ($hosts_to_ping as $host): ?>



<tr>

<td>

<?php echo $host; ?>

<?php $up = ping($host); ?>

</td>

<td>

<?php echo $up ? 'Connected' : 'Disconnected'; ?>

</td>

</tr>

<?php endforeach; ?>

</table>

</BODY>

</HTML>


This code works correctly as expected. All I've done is take this out of a test document and apply it to the PHPRunner project. I did print_r ($data["IP"]); and it does fill out the IP correctly. If there is some easier way to accomplish this I'd love to know because this is driving me nuts. All I'm trying to do is take the value in the IP filed on the _view.php page put that value into the $hosts_to_ping = array(IP HERE); and then print the word Connected or Disconnected. Thank you for your help thus far!

Admin 8/12/2011

These two code pieces are not the same (colon vs semicolon after foreach). Besides that using array doesn't make much sense if you only have a single IP address in 'IP' field.
If you need more help post your application to Demo Account and open a ticket at http://support.xlinesoft.com sending your Demo Account URL. 'Demo Account' button can be found on the last screen in the program. You need to have a valid support contract to do so.