もぐてっく

人は1つ歳をとるたび、1ビットづつ大きくなれると信じてた。

たった1行!シェルスクリプトからAmazon Dash Buttonを使う

tcpdumpコマンドを使ってAmazon Dash Buttonのボタン押下を検出する仕組みを編み出しました。

どうやんの?

tcpdump arp and ether src ac:63:be:ba:09:4b -c 1

 arp    :ARPパケット
 and    :かつ
 ether src ac:63:be:ba:09:4b:発信元MACアドレスがDash ButtonのMACアドレス
 -c 1 :該当するパケットを1回検出したらtcpdumpを終了する。

Dash Buttonを押すとtcpdumpが上記のパケットを検知して終了するので、その後にお望みの処理をするシェルスクリプトを書く感じです。

#! /bin/sh

while true ;do
	ssh -i $2 root@localhost tcpdump arp and ether src $1 -c 1 > /dev/null 2>&1
	echo "ボタンが押されました"
done

Linuxとかでtcpdumpを動かすにはroot権限が必要です。
今回は普通のユーザーから使いたかったので、sshの公開鍵認証でパスワードなしにrootに昇格させることにしています。

おまけ:Dash ButtonのMACアドレスを調べる

moguno@dashbutton:~$ sudo tcpdump -e arp -c 1
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 262144 bytes

(Dash Buttonを押す)

09:11:06.876606 ac:63:be:ba:09:4b (oui Unknown) > Broadcast, ethertype ARP (0x0806), length 60: Request who-has 192.168.11.1 tell 192.168.11.13, length 46

ac:63:be:ba:09:4bがMACアドレスです。