0

Hello guys, i have been away in the last few days, because of work, so i am on again.

I am trying to ping a series of hosts and then add info about the ping in a row.
I have a DB called ad, with a table called ping with row comp and row ping.
In row comp i have 400+ hostnames, and in row ping i need to insert the stat of that host. (if it is online or not)

I have the following code but if is not updating the row ping.

The simple PHP ping code is:

<?php
$comp = "127.0.0.1";
$str = exec("ping -n 1 -w 1 $comp", $input, $result);
if ($result == 0){
echo "Connected";
}else{
echo "Disconnected";
}
?>

This is a relatively simple code.
Now my modified code (that is not working):

<?php
include 'config_all.php';

    $con = mysql_connect("$sqlhost","$sqluser",""); 
    if (!$con) 
    { 
    die('Could not connect: ' . mysql_error()); 
    } 
$sel = mysql_select_db("ad", $con); 
    if (!$sel) 
    { 
    die('Could not select DB: ' . mysql_error()); 
    } 
$on = "On";
$off = "Off";

$comp = mysql_query("SELECT * FROM comp");
//$comp ="PT1n1894";
$str = exec("ping -n 1 -w 1 $comp", $input, $result);
if ($result == 0){

mysql_query("
            INSERT INTO 
                $table_ping (ping)
            VALUES 
                ('$on')
");
}else{

mysql_query("
            INSERT INTO 
                $table_ping (ping)
            VALUES 
                ('$off')
");
}
?>

I now that there is something wrong, but i cannot see... diafol can you help please?

2
Contributors
15
Replies
53
Views
3 Years
Discussion Span
Last Post by João_5
0

you have missed out the while loop.

$comp = mysql_query("SELECT comp or * FROM ping"); <-- I think you want row comp or both fields from table ping
while ($row = mysql_fetch_assoc($comp)) 
{
    $str = exec("ping -n 1 -w 1 $row['comp']", $input, $result);
    if ($result == 0)
    {
        mysql_query("
            INSERT INTO 
                $table_ping (ping)
            VALUES 
                ('$on')");
    }
    else
    {
        mysql_query("
            INSERT INTO 
                $table_ping (ping)
            VALUES 
                ('$off')");
    }
}
0

tested with a fiew errors, let me clarify, i have changed the db info:

DB: ad
Table: hoststate (with variable $table_ping)
Row with computer info: comp
Empty row where i should insert info: state

I Get this error:

Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in C:\xampp\htdocs\ping.php on line 21

Line 21 is:
$str = exec("ping -n 1 -w 1 $row['comp']", $input, $result);
Now can someone help?

Edited by João_5

0

sorry I forgot about quotes.
To keep your code similar to what you have before

do

$thiscomp = $row['comp'];
$str = exec("ping -n 1 -w 1 $thiscomp", $input, $result);
0

Like this?

    $con = mysql_connect("$sqlhost","$sqluser",""); 
    if (!$con) 
    { 
    die('Could not connect: ' . mysql_error()); 
    } 
$sel = mysql_select_db("ad", $con); 
    if (!$sel) 
    { 
    die('Could not select DB: ' . mysql_error()); 
    } 
$on = "On";
$off = "Off";
$thiscomp = $row['comp'];
$comp = mysql_query("SELECT comp or * FROM hostsate"); 
while ($row = mysql_fetch_assoc($comp)) 
{

$str = exec("ping -n 1 -w 1 $thiscomp", $input, $result);
    if ($result == 0)
    {
        mysql_query("
            INSERT INTO 
                $table_ping (ping)
            VALUES 
                ('$on')");
    }
    else
    {
        mysql_query("
            INSERT INTO 
                $table_ping (ping)
            VALUES 
                ('$off')");
    }
}
0

no the thiscomp variable must be inside the while loop.

$comp = mysql_query("SELECT comp or * FROM hostsate"); 
while ($row = mysql_fetch_assoc($comp)) 
{
$thiscomp = $row['comp'];
....
0

also change the mysql_query to

$comp = mysql_query("SELECT * FROM hostsate");

as I only put the OR in there as I didn't know if you wanted everything back from the table or just 1 field.

0

The intent is to fetch the hostname from row comp, ping and insert the results in row state.

Even with the changes that you made it is not working.

0

ok change your

        mysql_query("
            INSERT INTO 
                $table_ping (ping)
            VALUES 
                ('$on')");

                to

        mysql_query("
            UPDATE 
                hostsate <-- this is your table name
            SET
                ping="on" <-- this is the field you want to update
            WHERE
                comp=$thiscomp); <-- this will update the record with matching ip address
0

Error:
Parse error: syntax error, unexpected 'on' (T_STRING) in C:\xampp\htdocs\ping.php on line 29
Men i have been searching all day for this...

Edited by João_5

0

ok try this code. I have tested this using web addresses like www.google.co.uk etc and it all works.

<?php
    include 'config_all.php';
    $con = mysql_connect("$sqlhost","$sqluser","");

    if (!$con) 
    {
        die('Could not connect: ' . mysql_error());
    }

    $sel = mysql_select_db("ad", $con); 
    if (!$sel) 
    { 
        die('Could not select DB: ' . mysql_error()); 
    } 
    $comp = mysql_query("SELECT comp FROM pingtab");
    while ($row = mysql_fetch_assoc($comp)) 
    {
        $comptest = $row['comp'];
        $str = exec("ping -n 1 -w 1 $comptest", $input, $result);
        if ($result == 0)
        {   $updatequery = "UPDATE  `ad`.`ping` SET  `ping` =  'on' WHERE  `ping`.`comp` =  '$comptest' LIMIT 1";
            $working = mysql_query($updatequery);
        }
        else
        {
            $updatequery = "UPDATE  `ad`.`ping` SET  `ping` =  'off' WHERE  `ping`.`comp` =  '$comptest' LIMIT 1";
            $working = mysql_query($updatequery);

        }
    }
?>
0

Noel, thanks

its working now
DB: ad
Table: hoststate
Affected row: ping
Info row: comp

You added: $updatequery = "UPDATEad.pingSETping= 'on' WHEREping.comp= '$comptest' LIMIT 1";
And was not working
I changed to: $updatequery = "UPDATEhoststateSETping= 'on' WHEREcomp= '$comptest'";
And its working now.

I could not have done it without you...

Thanks a lot

Edited by João_5

0

Now i need to set the exceution time to the max that i can.

here is the code that i use:

$seconds = '60';
ini_set('max_execution_time', $seconds);

Now is set to 1 minute (60 secounds), but still is not enough.
As my quary as 100+ hostnames i will test 300 secounds.

0

I also adde4d this line just to clean the table ping before updating

mysql_query("UPDATE `hoststate` SET `ping`= NULL WHERE `ping` is not null");
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.