Click here to Skip to main content
14,539,164 members

Amazon Dash Button Detection via DHCP Request

Rate this:
5.00 (2 votes)
Please Sign up or sign in to vote.
5.00 (2 votes)
4 Oct 2016CPOL
Powershell script for Amazon Dash button Detection via DHCP Discover message

Introduction

This is a simple snippet to listen for DHCP Request messages that come from Amazon Dash buttons; for the purpose of Dash button automation. Most solutions require either intercepting the request to button's request to Amazon or pinging a known IP address. Both of these solutions tend to require additional setup of the router to detect and redirect the button request, or a static DHCP reservation to keep the dash button's IP address consistent. Both of the aforementioned solutions can have as much as a 5.5 second delay between when the button is first pressed to when it is detected. This method of intercepting the broadcast DHCP Request can take as little as 2.5 seconds. Previously, this code only looked for DHCP Discover messages, however some dash buttons do not always broadcast this packet.

Using the Code

You will need to know the mac address of the dash button so that the filter can be setup. The script will echo to the text file all the mac addresses that broadcast a DHCP Request address making it easy to figure out which mac address goes to which button.

#log script obtained from
#https://community.spiceworks.com/topic/1233789-add-logging-to-powershell-script
$LogPath = Split-Path $MyInvocation.MyCommand.Path
Get-ChildItem "$LogPath\*.log" | 
Where LastWriteTime -LT (Get-Date).AddDays(-15) | Remove-Item -Confirm:$false
$LogPathName = Join-Path -Path $LogPath 
-ChildPath "$($MyInvocation.MyCommand.Name)-$(Get-Date -Format 'MM-dd-yyyy').log"
Start-Transcript $LogPathName -Append

$StartDate = (Get-Date)

#dash button script. Change the $dashButtonMac variable to the address of the dash button.

$dashButtonMac = "74:75:48:9b:ad:fb"

$endpoint = new-object System.Net.IPEndPoint ([system.net.ipaddress]::any, 67)
$listenHost = new-object System.Net.Sockets.UdpClient
$listenHost.Client.SetSocketOption([System.Net.Sockets.SocketOptionLevel]::Socket, 
[System.Net.Sockets.SocketOptionName]::ReuseAddress, $True);
$bindEndPoint = new-object System.Net.IPEndPoint([system.net.ipaddress]::any, 67)
$listenHost.client.Bind($bindEndPoint)

$delay = Get-Date

while($true){
    $payload = $listenHost.Receive([ref] $endpoint)
    if($payload[0] -eq 1 -and $payload[242] -eq 3 -and ((Get-Date) - $delay).seconds -gt 5)
          { #filter for DHCP Request messages only. byte 242 checks option 53's value of 3
        $delay = Get-Date
        $clientMac = 0,0,0,0,0,0
        [array]::Copy($payload, 28,$clientMac,0,6)
        for($i = 0; $i -lt $clientMac.Count; $i++){
            $clientMac[$i] = "{0:x2}" -f $clientMac[$i]
        }
        $mac = $clientMac -join ':'
        if($mac -eq $dashButtonMac){
            Write-Output ("Dash Button has been detected : " + $mac)
            # do something
        }
        else{
            Write-Output ("Valid DHCP Request message obtained but not from Dash button : " + $mac)
            # optionaly do something else
        }
    }
    Start-Sleep -Milliseconds 100
}

History

  • First published working script
  • Modified to look for DHCP Request in place of DHCP Discover messages

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Shawn K Lewis
Trinidad and Tobago Trinidad and Tobago
No Biography provided

Comments and Discussions

 
Praise:) Pin
Emma794-Oct-16 14:51
MemberEmma794-Oct-16 14:51 
Praise:) Pin
Emma794-Oct-16 14:51
MemberEmma794-Oct-16 14:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Tip/Trick
Posted 4 Oct 2016

Tagged as

Stats

11.1K views