Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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{myEmail@gmail.com};
my $pass  = q{myPass};
my $sms   = q{myPhoneNumber@tmomail.net};

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.

share|improve this question
    
Based on my reading, it looks like this functionality may not be possible, at least naively: bugzilla.gnome.org/show_bug.cgi?id=387832 –  Dang Khoa Sep 21 '11 at 6:38
    
I think the script is called (ad can only be called) when a network down event has already be triggered. Think of a suddenly disconnected network cable: how can something be executed before? –  enzotib Sep 21 '11 at 8:02
    
@enzotib - exactly the point. The bug discussion I linked to points out this problem at length - it's easy to run a pre-disconnect script when the computer is initiating the disconnect; but it can't handle cables unplugged, APs going out of range, or hard power-off. We as users can do our best to make sure things fail predictably, however? In any case, I (obviously) won't be using the pre-disconnect script for the purpose I had intended originally. –  Dang Khoa Sep 21 '11 at 8:21
    
Vote to close - there can not possibly be any straightforward solution to this answer that wouldn't elicit extended discussion, etc. –  Dang Khoa Sep 21 '11 at 8:24
add comment

1 Answer

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.