Monitor login attempts on your home WiFi

Infinite Future

The infinite loop in lines 28-40 of Listing 2 checks the DHCP lease file on the router every 30 seconds to discover whether the list with the MAC addresses extraced using the cut command results in a different sha1sum footprint than the previous run. To maintain that value even through a reboot, line 36 stores the current value in the router's non-volatile RAM using nvram set. nvram get in line 31 picks the value up again to compare it with the current value from the lease file and transfers the current file to the server in case of changes.

To make sure the router runs the script after every reboot, I dropped it in the web UI in the Startup text field below Administration | Commands and pressed the Save Startup button at the bottom of the page (Figure 3). If something goes wrong, it isn't easy to discover why; the slimmed down commands on DD-WRT do not provide very much in the way of detailed error messages, but if you log in to the router using SSH and run the script manually at the command line, you can normally find the culprit.

Figure 3: This script periodically sends changed lease entry files to the server.

Messenger

The router thus ensures that the current version of the lease file is always available on a Linux server where a cron job,

* * * * * /home/perlsnapshot/lease-notify

running once a minute, detects the changes and notifies the Prowl web service about events, like clients joining or leaving the WiFi.

The Prowl service requires users to register on the Prowl website [2] with a username and password, where you do not need to provide an email address; however, it is useful later if you forget your password. An account entitles you to obtain API keys with which applications that want to send events authenticate against the web service. The Prowl server forwards the text messages to all instances of the app registered under the account. The app costs $2.99 for an iPhone, but registering on the website to pick up an API key and forwarding the push notifications are both free for up to 1,000 events sent per day.

Link If Needed

The simple test included in Listing 3 shows the procedure that uses the CPAN WebService::Prowl module, which nicely abstracts access to the Prowl web service. It requires an API key, which is available on the website under the API Keys tab as a text string for the logged-in user (Figure 4). The verify() method in line 8 checks whether communication to the Prowl server is working, and the add() method in line 10 accepts the four text strings for the message to be sent over the air.

Listing 3

prowl-test

 

Figure 4: Free API keys from Prowl.

The first three parameters provide the name of the sending application, the type of event, and a short description. The fourth parameter, url, carries a URL that the cell phone is supposed to access when the user taps on the event and then consents to launching the browser. In Figure 5, you can see the push notification for the test event from Listing 3 just arriving on my locked iPhone 5. Figure 6 shows the dialog that appears when the user taps on the event.

Figure 5: The iPhone showing the test message on the lock screen.
Figure 6: Opening the URL for the event.

On the server side, the script in Listing 4 runs as a cron job. It maintains a persistent data repository in the leases.dat file, to which it binds the %leases hash in line 14 using tie. After lease-notify is launched, the program looks for a file named dnsmasq.leases, loads its data using the CPAN Path::Tiny module, and then iterates through its text with lines() in line 20. The split() function in line 22 breaks down the space-separated fields into the remaining lease duration, MAC address, issued IP address, and device name.

Listing 4

lease-notify

 

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Programming Snapshot – Power Outages

    A power failure can cause the IQ of a smart home to plummet suddenly. An emergency power supply and a script on the SmartThings platform can prevent a total outage and inform the owner. The polyglot Perlmeister embarks on a foray into the territory of the Groovy scripting language in this issue.

  • Dnsmasq

    Dnsmasq is a practical alternative for DNS on a small scale.

  • A Python script warns of failed login attempts

    A number of sensors and cameras send author Mike Schilli a short message if someone tampers with his apartment door. He has now applied this security principle to the SSH entrance of his Linux computer.

  • TECH TOOLS

    Professional users are always searching for an edge. Whether you work with Linux as a webmaster, programmer, system administrator, or security consultant, you know the best solution depends on finding the right tool for the job. We thought you might be interested in the following new products and updates.

  • Gaping Hole in DD-WRT: Router Software with Back Door

    The free router software DD-WRT opens in its version 24(SP1) a huge door due to a vulnerability in its HTTP daemon server.

comments powered by Disqus

Direct Download

Read full article as PDF:

Price $2.95

News