I am trying to use /etc/network/if-up.d/
and /etc/network/if-down.d
to send an SMS from my computer to my phone via Gmail whenever my computer enters or exits a wireless network. The main script is written in Perl, and the scripts in said folders are essentially wrappers around it. Here is the Perl script, ~user/bin/laptopSMS.pl
:
#!/usr/bin/perl
use strict;
use warnings;
use Email::Send;
use Email::Send::Gmail;
use Email::Simple::Creator;
my $addy = q{[email protected]};
my $pass = q{myPass};
my $sms = q{[email protected]};
my $email = Email::Simple->create(
header => [
From => $addy,
To => $sms,
Subject => "$ARGV[0]",
],
body => "$ARGV[1] / $ARGV[2]",
);
my $sender = Email::Send->new(
{ mailer => 'Gmail',
mailer_args => [
username => $addy,
password => $pass,
]
}
);
eval { $sender->send($email) };
die "Error sending email: $@" if $@;
The following script is /etc/network/if-up.d/99postconnect_laptopSMS
:
#!/bin/bash
set -e
if [ "$IFACE" == "wlan0" ]; then
ipaddr=`ip addr | grep inet[^6] | grep -v 127.0.0.1 | awk {'print $2'} | sed 's!/[0-9]*!!g'`
ssid=`iwgetid -r`
~user/bin/laptopSMS.pl "connected" "$ssid" "$ipaddr"
fi
This works fine, and I get a text message whenever my phone enters a network; the corresponding if-down.d
script to send a "disconnect" signal, however, does not:
#!/bin/bash
set -e
if [ "$IFACE" == "wlan0" ]; then
~user/bin/laptopSMS.pl "disconnect" "disconnect" "disconnect"
fi
This is not executing; I tried to put the following snippet into the script to see if anything is actually running:
ifdowntest=/home/user/ifdown_test.txt
date >> $ifdowntest
whoami >> $ifdowntest
ifconfig >> $ifdowntest
sleep 5
Leaving this in the script by itself executes just fine, but appending it to the wrapper to the Perl script does not. (No file is generated.)
The output of ifconfig >> $ifdowntest
shows that wlan0 does not have an IP - it's been taken down. Can I get this script to execute pre-down (I thought that's what the if-down.d
folder did) or somehow edit something to make this work? Note that it takes about 3 seconds for the Perl script to fire when running it manually.
FWIW: I'm taking down the network manually by clicking the NetworkManager applet and clicking "Disconnect". I could potentially just kill my router to test that too, but I don't know if there's a difference between manually disconnecting and the AP no longer existing.
Try to move your "disconnect" script to /etc/network/if-post-down.d, that's the directory that should actually get run when NetworkManager disconnects, rather than if-down.d (which would mean triggering something as the device goes down, not when it just disconnected). For what you're trying to achieve, it shouldn't be an issue, and in fact avoid trying to send the SMS via network while the default gateway might not yet have been updated.
This obviously assumes you then still have another connection (wired) to send the SMS over. Anything else is too uncertain and unreliable to be worth using (you can't expect the wireless connection to be working enough by the time the scripts are run to send an SMS over it).
If you want to investigate further into this, you can look at /etc/NetworkManager/dispatcher.d/01ifupdown.