Results 1 to 8 of 8

Thread: make ping with PHP

  1. #1
    Join Date
    Jul 2008
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default make ping with PHP

    hi! I put this PHP file into my apache server:


    Code:
    <?php
    $ip = $_SERVER['127.0.0.1'];
    exec("ping -n 4 $ip 2>&1", $output, $retval);
    if ($retval != 0) { 
    echo "no!"; 
    } 
    else 
    { 
    echo "yes!"; }
    ?>
    I would like to see that's my server is good or dead, and it returns: NO!

    why???

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Because $_SERVER['127.0.0.1'] doesn't exist — you just meant '127.0.0.1', I'm sure. If you had put error_reporting(E_ALL); at the top of your script, you would have got a handy warning about this. I recommend that you do so for all PHP scripts.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Jul 2008
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    well, i would like to monitoring diferetns servers.
    Why not's possible????
    127.0.0.1 is the ip of all lan targets...

  4. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    No, 127.0.0.1, or 'localhost', is the standard address of the current machine. If you wish to monitor other servers, you will have to replace that with the remote machine's IP address.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  5. #5
    Join Date
    Jul 2008
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I change this code and I put this:

    Code:
    IP: 10.140.206.123 (
    <?php
    $ping_ex = exec("ping -n 1 10.110.206.123", $ping_result, $pr);
    if (count($ping_result) > 1){
    echo "<img src=\"on.png\">";
    } else{
    echo "<img src=\"off.png\">";
    }
    ?> )
    
    
    IP: 127.0.0.1 (
    <?php
    $ping_ex = exec("ping -n 1 127.0.0.1", $ping_result, $pr);
    if (count($ping_result) > 1){
    echo "<img src=\"on.png\">";
    } else{
    echo "<img src=\"off.png\">";
    }
    ?> )
    In the first case the resulst are NO and the second case are YES.
    But... the result is YES in two cases. Why???
    Last edited by guif; 10-08-2008 at 01:28 PM.

  6. #6
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Because now you're just checking that there's output, rather than what actually happened.
    Code:
    <?php
      function ping($host) {
        exec(sprintf('ping -c 1 -W 5 %s', escapeshellarg($host)), $res, $rval);
        return $rval === 0;
      }
    
      $hosts_to_ping = array('10.140.206.123', '127.0.0.1');
    ?>
    
    <ul>
    <?php foreach ($hosts_to_ping as $host): ?>
      <li>
        <?php echo $host; ?>
        <?php $up = ping($host); ?>
        (<img src="<?php echo $up ? 'on' : 'off'; ?>"
              alt="<?php echo $up ? 'up' : 'down'; ?>">)
      </li>
    <?php endforeach; ?>
    </ul>
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  7. #7
    Join Date
    Jul 2008
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    yeah!
    finally I put this code:

    Code:
    <?php
    $str = exec("ping -n 1 -w 1 100.100.100.100", $input, $result);
    if ($result == 0){
    echo "<img src=\"on.png\">";
    }else{
    echo "<img src=\"off.png\">";
    }
    ?>
    It's run correctly, but i would like to insert this code into a javascript function because I insert many times this PHP and the page is very hard to load.
    I would like to insert a button:

    Code:
    <input type="submit" onClick="para();">
    and when I press this button, the javascript runs.
    Is it possible??

  8. #8
    Join Date
    Nov 2008
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    What you wish to do could be possible by using an iframe.

    It could originally point to a blank file so it wont have to load the ping. Then javascript can be used to change the address of the iframe

    Code:
    <script type="text/javascript">
    
    function Reload(address) {
    var f = document.getElementById("iframe1");
    f.src = address;
    }
    
    </script>
    Just call that function when u want to ping. e.g.
    Code:
    <input type="button" onClick="Reload('ping.php?ip=100.100.100.100');">

    You could also make this iframe invisible when not used...
    Code:
    <div id="div1" style="display: none;">
    <iframe goes here>
    </div>
    And toggle visibility with:
    Code:
    <script language="javascript">
    <!--
    
    var state = 'none';
    
    function showhide(layer_ref) {
    
    if (state == 'block') {
    state = 'none';
    }
    else {
    state = 'block';
    }
    if (document.all) { //IS IE 4 or 5 (or 6 beta)
    eval( "document.all." + layer_ref + ".style.display = state");
    }
    if (document.layers) { //IS NETSCAPE 4 or below
    document.layers[layer_ref].display = state;
    }
    if (document.getElementById &&!document.all) {
    hza = document.getElementById(layer_ref);
    hza.style.display = state;
    }
    }
    //-->
    </script>
    <a href="javascript:showhide('div1');">Show/hide</a>
    Hope this helps!
    Nick

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •