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.
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
01 #!/usr/local/bin/perl -w 02 use strict; 03 04 use WebService::Prowl; 05 my $ws = WebService::Prowl->new( 06 apikey => "xxxxxxxxxxxxxxxxxxxxxxx"); 07 08 $ws->verify || die $ws->error(); 09 10 $ws->add( 11 application => "Perl Snapshot", 12 event => "Just a test.", 13 description => "Huzzah, it works!", 14 url => "http://linux-magazin.de", 15 );
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.
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
01 #!/usr/bin/perl -w 02 use strict; 03 use lib '/home/perlsnapshot/perl5/lib/perl5'; 04 use Path::Tiny; 05 use DB_File; 06 use WebService::Prowl; 07 use File::Basename; 08 09 my $in_file = "dnsmasq.leases"; 10 my $db_file = "leases.dat"; 11 my $API_KEY = "xxxxxxxxxxxxxxxxxxxxxxx"; 12 my $APP_NAME = "Wifi watch"; 13 14 tie my %leases, 'DB_File', $db_file; 15 16 my $file = path( $in_file ); 17 18 my %found = (); 19 20 for my $line ( $file->lines ) { 21 my( $secs, $mac, $ip, $name ) = 22 split " ", $line; 23 24 $found{ $mac } = 1; 25 26 if( !exists $leases{ $mac } ) { 27 $leases{ $mac } = "$ip $name"; 28 notify( "joined: $leases{ $mac }" ); 29 } 30 } 31 32 for my $mac ( keys %leases ) { 33 if( !exists $found{ $mac } ) { 34 notify( "left: $leases{ $mac }" ); 35 delete $leases{ $mac }; 36 } 37 } 38 39 untie %leases; 40 41 ########################################### 42 sub notify { 43 ########################################### 44 my( $event ) = @_; 45 46 my $ws = WebService::Prowl->new( 47 apikey => $API_KEY ); 48 49 $ws->verify || die $ws->error(); 50 51 $ws->add( 52 application => $APP_NAME, 53 event => $event, 54 description => "Home Wifi clients", 55 url => "", 56 ); 57 }
« Previous 1 2 3 Next »
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
OSJH and LPI Release 2024 Open Source Pros Job Survey Results
See what open source professionals look for in a new role.
-
Proton 9.0-1 Released to Improve Gaming with Steam
The latest release of Proton 9 adds several improvements and fixes an issue that has been problematic for Linux users.
-
So Long Neofetch and Thanks for the Info
Today is a day that every Linux user who enjoys bragging about their system(s) will mourn, as Neofetch has come to an end.
-
Ubuntu 24.04 Comes with a “Flaw"
If you're thinking you might want to upgrade from your current Ubuntu release to the latest, there's something you might want to consider before doing so.
-
Canonical Releases Ubuntu 24.04
After a brief pause because of the XZ vulnerability, Ubuntu 24.04 is now available for install.
-
Linux Servers Targeted by Akira Ransomware
A group of bad actors who have already extorted $42 million have their sights set on the Linux platform.
-
TUXEDO Computers Unveils Linux Laptop Featuring AMD Ryzen CPU
This latest release is the first laptop to include the new CPU from Ryzen and Linux preinstalled.
-
XZ Gets the All-Clear
The back door xz vulnerability has been officially reverted for Fedora 40 and versions 38 and 39 were never affected.
-
Canonical Collaborates with Qualcomm on New Venture
This new joint effort is geared toward bringing Ubuntu and Ubuntu Core to Qualcomm-powered devices.
-
Kodi 21.0 Open-Source Entertainment Hub Released
After a year of development, the award-winning Kodi cross-platform, media center software is now available with many new additions and improvements.