Script to check MAC addresses (connected/disconnected) and internet connectivity [via e-mail]


  • administrators

    Greetings,
    
    I'm not sure if there's already an application for this. If there is none, I want to request a script that runs on my server at home. It should detect if specific devices (identified by their MAC address) is connected to the local network. The script then should be able to notify me even if I am outside. Our home network is behind a CGNAT so I won't be able to connect to the router remotely.
    
    Another thing is, there is a possibility that there is no internet connection at home, a blackout could have happened, or anything else. The script won't be able to notify me. I wanted the script to have two channels of notification. One for device detection and the other for uptime.
    
    Nofitications:
    1. Device detection
    If a specific device is connected/disconnected to the home network, a notification should be sent immediately. In case of internet disconnection, the script should gather the state of device's connection/disconnection over time. Once internet connection is up, the gathered states should be sent to me.
    2. Uptime
    The script should periodically notifiy me if it can connect to the internet.
    
    Methods for notification:
    1. Email - This could get a little spammy with uptime notification.
    2. Telegram self-send?
    3. Something else?
    
    This was a bit long. I hope it's not too much to ask. Thank you for your time.
    

  • administrators

    Script location : https://git.shell5.dev/shell5dev/installation-scripts/blob/master/djedi-monitor-mac-internet/
    Mirror: https://github.com/shell5dev/Installation-Scripts/tree/master/djedi-monitor-mac-internet

    Methods for notification:
    3 👍

    Slack/Mattermost hook, since Email would cause excessive spam, Telegraf could be implemented but Slack seems better since you can mute the channel and yet still see notification number or send large log (not sure of Telegraf's limits per message).

    1. Uptime
      The script should periodically notifiy me if it can connect to the internet.

    For that purpose you can utilize the function from within the script and make separate "internet.sh":

    #!bin/bash
    scannetwork() {
      log "Starting scan for connecitivty to $URL"
      CHECK_CONNECTIVITY="$(wget -q --tries=1 --timeout=10 --spider $URL)"
      echo $?
      if [[ $? -eq 0 ]]; then
      net_result='internet'
      else
        net_last_result='nointernet'
      fi
      log "Got net result [${net_result}]"
    }
    

    Lets adapt it to send notification for example :

    netcheck() {
      CHECK_CONNECTIVITY="$(wget -q --tries=1 --timeout=10 --spider https://google.com)"
      echo $?
      if [[ $? -eq 0 ]]; then
      echo "there is internet, sending notification to slack"
        curl -X POST --data-urlencode "payload={\"channel\": \"#CHANNELHERE\", \"username\": \"USERNAMEHERE\", \"text\": \"I have internet .\", \"icon_emoji\": \":ghost:\"}" https://hooks.slack.com/services/#THEHOOK
      else
      echo "There is no internet :("
        
      fi
    }
    
    netcheck
    

    From this point you can make a cron job to send you notification e.g every 30 minutes:

    crontab -e
    */30 * * * * /bin/bash /home/$USER/internet.sh

  • administrators

    Also please bare in mind that some devices do have hibernation when they are not actively used. For example during testing, my iPhone was appearing and disappearing even though the WiFi was enabled - background fetching rendered it visible for short period of time and it was mostly preserving battery. I do not doubt that many/all mobile devices do it so you will have lots of noise in logs.


Log in to reply