Checking (and verifying) the internet on a raspberry pi with a bash script

This is how I check the internet with a bash script on a raspberry pi. It verifies the internet by downloading a file from a webserver (e.g http://www.your-domain/pi/test/simple_text_file.txt
(change this to a file of your choice)).

Script: CheckInternet.sh

    #!/bin/bash
    printf "BASH Version: $BASH_VERSION\n"

    printf "Testing Internet Connection (http://www.your-domain.com):\n\n"
    # ping -q -t 10 -w1 -c1 www.your-domain.com
    wget -q --tries=2 --timeout=20 http://www.your-domain.com
    if [[ $? -eq 0 ]]; then
    printf "Internet appears online.\n\n"
    TMP_FILE="./simple_text_file.txt"
    if [ -f $TMP_FILE ]
    then
    echo "Deleting old temp files.."
    sudo rm ./simple_text_file.txt
    fi
    printf "Downloading file from the internet.\n"
    sudo wget http://www.your-domain/pi/test/simple_text_file.txt
    if [ -f $TMP_FILE ]
    then
    printf "File downloaded ok.\n"
    printf "File Contents:\n"
    echo "----------------------------------------------------------"
    printf "\n"
    cat $TMP_FILE
    printf "\n\n"
    echo "----------------------------------------------------------"
    else
    printf "File failed to download.\n"
    fi
    else
    printf "Internet appears offline, unable to download files."
    fi

    printf "Tiding up..\n\n"
    sudo rm ./simple_text_file.txt

    printf "Goodbye\n\n"

To call this script from the command line do this.

    sudo bash CheckInternet.sh

Alternatively you can schedule emails by adding this to your “/etc/crontab” file


    # run every 15 minutes
    */15 * * * * root bash /usr/bin/simon/CheckInternet.sh

    # run every 5 minutes
    */5 * * * * root bash /usr/bin/simon/CheckInternet.sh

Or you can run this on pi start-up by adding the python call to your “/etc/rc.local” file.

    bash /usr/bin/simon/CheckInternet.sh

Make sure you make you file executable.

Send Email from Raspberry Pi

This is how I am sending emails (boot and 15 min runtime notifications) from my Pi

Python Script: /usr/bin/simon/SendEmail.py

You will need to type in your mail server Username/Password/IP/Port to get this to work.


    #!/usr/bin/python
    import smtplib

    EMAIL_USER = 'your-email@your-domain.com'
    EMAIL_PASS = 'your_email_password_goes_here'
    SMTP_SERVER = '192.0.0.1'
    SMTP_PORT = 587

    def send_email(recipient, subject, msg):
    smtpserver = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
    smtpserver.set_debuglevel(0)
    smtpserver.ehlo()
    smtpserver.starttls()
    smtpserver.login(EMAIL_USER, EMAIL_PASS)
    header = 'To:' + recipient + '\n' + 'From:' + EMAIL_USER + '\n' + 'Subject:' + subject + '\n'
    header = header + '\n' + msg + '\n\n'
    msg = header + '\n' + msg + '\n\n'
    smtpserver.sendmail(EMAIL_USER, recipient, msg)
    smtpserver.close()

    send_email('recipient-name@gmail.com','Subject Goes Here', 'Email contents goes here.')

To call this script from the command line do this.

    sudo python SendEmail.py

Alternatively you can schedule emails by adding this to your “/etc/crontab” file


    # run every 15 minutes
    */15 * * * * root python /usr/bin/simon/SendEmail.py

    # run every 5 minutes
    */5 * * * * root python /usr/bin/simon/SendEmail.py

Or you can run this on pi start-up by adding the python call to your “/etc/rc.local” file.

    python /usr/bin/simon/SendEmail.py