Skip to content

Instantly share code, notes, and snippets.

@xhip
Last active April 11, 2017 19:34
Show Gist options
  • Save xhip/8b133a3020ba4a3f8077e609a2e2d6e2 to your computer and use it in GitHub Desktop.
Save xhip/8b133a3020ba4a3f8077e609a2e2d6e2 to your computer and use it in GitHub Desktop.
Bash + PHP + cron = making a quick and dirty ping system

PHP PING - quick and dirty

NOTE:

This isn't the best way to do it! This is only for my case.

Idea:

I want to receive notifications if my laptop gets disconnect from the internet.

Tools:

  • a folder on Apache Server (w/ PHP)
  • a limited user account for that server. (no root, I can only use what is installed on that machine)

Files Client/Laptop:

  • cron

    Every X minutes run "send_ping.sh" that sends a request to the server.

    */X * * * * bash <path>/send_ping.sh
    

Files Server:

  • index.php

    Gives how many minutes since last ping, just for control.

    <?php
        $FILE = file_get_contents('./<File>.txt', true);
        $NOW = strtotime(date("Y-m-d h:i"));
        $PING = strtotime($FILE);
        $X = round(abs($NOW - $PING) / 60,2);
    ?>
    <html>
            <head>
                    <title> Ping </title>
            </head>
            <body>
                    <h1>Last Ping: <?php echo $X ?> Minutes</h1>
            </body>
    </html>
    
  • ping_[random string].php

    Receives requests and check if everything is ok, update the timestamp on X file.

  • check.sh

    Check X file for how many minutes since the last ping, IF [ TOTAL > ex 5 minutes ] then sends a notification to my cellphone.

  • cron

    Every X minutes run "check.sh"

    */X * * * * bash <path>/check_ping.sh
    

TODO

  • get a private VPS ;) so I don't need this
  • play with mobile connections
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment