Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jwalanta/53f55d03fcf5265938b64ffd361502d5 to your computer and use it in GitHub Desktop.
Save jwalanta/53f55d03fcf5265938b64ffd361502d5 to your computer and use it in GitHub Desktop.

Revisions

  1. jwalanta revised this gist Jan 23, 2017. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions OpenWrt detect new device and send text message.md
    Original file line number Diff line number Diff line change
    @@ -32,7 +32,7 @@ if [ "$1" == "add" ]; then
    echo `date` $msg >> /tmp/dhcpmasq.log
    # encode colon (:) and send email
    echo $msg | sed s/:/%3A/g | sendmail "$notification_email"
    echo $msg | sed s/:/-/g | sendmail "$notification_email"
    fi
    ```

    @@ -64,7 +64,7 @@ if [ "$1" == "add" ] && [ "$unknown_mac_addr" -ne 0 ]; then
    echo `date` $msg >> /tmp/dhcpmasq.log
    # encode colon (:) and send email
    echo $msg | sed s/:/%3A/g | sendmail "$notification_email"
    echo $msg | sed s/:/-/g | sendmail "$notification_email"
    fi
    ```

  2. jwalanta revised this gist Jan 23, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion OpenWrt detect new device and send text message.md
    Original file line number Diff line number Diff line change
    @@ -68,4 +68,4 @@ if [ "$1" == "add" ] && [ "$unknown_mac_addr" -ne 0 ]; then
    fi
    ```

    When a new device is added, dnsmasq calls `detect_new_device.sh` with arguments `add mac_addr ip_addr devicename`. We check if the device is new (if the dhcp lease hasn't expired, it calls with `old`), then logs and emails (which eventually is a text message) the information.
    When a new device is added, dnsmasq calls `detect_new_device.sh` with arguments `add mac_addr ip_addr devicename`. The script checks if the device is new (if the dhcp lease hasn't expired, it calls with `old`), then logs and emails (which eventually is a text message) the information.
  3. jwalanta revised this gist Jan 23, 2017. 1 changed file with 46 additions and 1 deletion.
    47 changes: 46 additions & 1 deletion OpenWrt detect new device and send text message.md
    Original file line number Diff line number Diff line change
    @@ -18,8 +18,53 @@ Reference:
    # script to detect new dhcp lease
    # this will be called by dnsmasq everytime a new device is connected
    # with the following arguments
    # $1 = add | old
    # $2 = mac address
    # $3 = ip address
    # $4 = device name
    notification_email="[email protected]"
    if [ "$1" == "add" ]; then
    echo "New device on `uci get system.@system[0].hostname`.`uci get dhcp.@dnsmasq[0].domain` $* `date`" | tee -a /tmp/dhcpmasq.log | sed s/:/%3A/g | sendmail [email protected]
    msg="New device on `uci get system.@system[0].hostname`.`uci get dhcp.@dnsmasq[0].domain` $*"
    echo `date` $msg >> /tmp/dhcpmasq.log
    # encode colon (:) and send email
    echo $msg | sed s/:/%3A/g | sendmail "$notification_email"
    fi
    ```

    #### Alternative script using whitelist

    This script only sends alerts if the mac address is not in the list

    ```
    #!/bin/sh
    # script to detect new dhcp lease
    # this will be called by dnsmasq everytime a new device is connected
    # with the following arguments
    # $1 = add | old
    # $2 = mac address
    # $3 = ip address
    # $4 = device name
    known_mac_addr="/etc/known_mac_addr"
    notification_email="[email protected]"
    # check if the mac is in known devices list
    grep -q "$2" "$known_mac_addr"
    unknown_mac_addr=$?
    if [ "$1" == "add" ] && [ "$unknown_mac_addr" -ne 0 ]; then
    msg="New device on `uci get system.@system[0].hostname`.`uci get dhcp.@dnsmasq[0].domain` $*"
    echo `date` $msg >> /tmp/dhcpmasq.log
    # encode colon (:) and send email
    echo $msg | sed s/:/%3A/g | sendmail "$notification_email"
    fi
    ```

  4. jwalanta revised this gist Jan 22, 2017. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions OpenWrt detect new device and send text message.md
    Original file line number Diff line number Diff line change
    @@ -1,17 +1,17 @@
    * Add the following line in /etc/dnsmasq.conf
    ### Add the following line in `/etc/dnsmasq.conf`

    `dhcp-script=/etc/detect_new_device.sh`


    * Setup sendmail to send email to your text number.
    ### Setup sendmail to send email to your text number.

    Reference:

    - https://wiki.openwrt.org/doc/howto/smtp.client
    - https://fixmynix.com/send-mail-command-line-linux-openwrt/
    - http://lifehacker.com/5506326/how-can-i-send-an-email-via-text-message

    * Create `/etc/detect_new_device.sh` with the following content
    ### Create `/etc/detect_new_device.sh` with the following content

    ```
    #!/bin/sh
  5. jwalanta revised this gist Jan 22, 2017. 1 changed file with 5 additions and 3 deletions.
    8 changes: 5 additions & 3 deletions OpenWrt detect new device and send text message.md
    Original file line number Diff line number Diff line change
    @@ -1,15 +1,17 @@
    ## Add the following line in /etc/dnsmasq.conf
    * Add the following line in /etc/dnsmasq.conf

    `dhcp-script=/etc/detect_new_device.sh`


    ## Setup sendmail to send email to your text number. Reference
    * Setup sendmail to send email to your text number.

    Reference:

    - https://wiki.openwrt.org/doc/howto/smtp.client
    - https://fixmynix.com/send-mail-command-line-linux-openwrt/
    - http://lifehacker.com/5506326/how-can-i-send-an-email-via-text-message

    ## Create `/etc/detect_new_device.sh` with the following content
    * Create `/etc/detect_new_device.sh` with the following content

    ```
    #!/bin/sh
  6. jwalanta revised this gist Jan 22, 2017. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions OpenWrt detect new device and send text message.md
    Original file line number Diff line number Diff line change
    @@ -1,15 +1,15 @@
    # Add the following line in /etc/dnsmasq.conf
    ## Add the following line in /etc/dnsmasq.conf

    `dhcp-script=/etc/detect_new_device.sh`


    # Setup sendmail to send email to your text number. Reference
    ## Setup sendmail to send email to your text number. Reference

    - https://wiki.openwrt.org/doc/howto/smtp.client
    - https://fixmynix.com/send-mail-command-line-linux-openwrt/
    - http://lifehacker.com/5506326/how-can-i-send-an-email-via-text-message

    # Create `/etc/detect_new_device.sh` with the following content
    ## Create `/etc/detect_new_device.sh` with the following content

    ```
    #!/bin/sh
  7. jwalanta created this gist Jan 22, 2017.
    24 changes: 24 additions & 0 deletions OpenWrt detect new device and send text message.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    # Add the following line in /etc/dnsmasq.conf

    `dhcp-script=/etc/detect_new_device.sh`


    # Setup sendmail to send email to your text number. Reference

    - https://wiki.openwrt.org/doc/howto/smtp.client
    - https://fixmynix.com/send-mail-command-line-linux-openwrt/
    - http://lifehacker.com/5506326/how-can-i-send-an-email-via-text-message

    # Create `/etc/detect_new_device.sh` with the following content

    ```
    #!/bin/sh
    # script to detect new dhcp lease
    if [ "$1" == "add" ]; then
    echo "New device on `uci get system.@system[0].hostname`.`uci get dhcp.@dnsmasq[0].domain` $* `date`" | tee -a /tmp/dhcpmasq.log | sed s/:/%3A/g | sendmail [email protected]
    fi
    ```

    When a new device is added, dnsmasq calls `detect_new_device.sh` with arguments `add mac_addr ip_addr devicename`. We check if the device is new (if the dhcp lease hasn't expired, it calls with `old`), then logs and emails (which eventually is a text message) the information.