OS Xで無線LANに接続したことを検出するscript。引数に
インタフェース名,起動するコマンド
と指定すると、接続/切断したタイミングでコマンドを起動する。
この時、引数-s status -e ESSID
をつける。
stausは、接続時 TRUE/切断時 FALSEとなる。
ESSIDは接続したネットワークのESSID。
公衆無線LANで使うには、Hotspot Connector を呼び出すwrapperを用意するといいかも。
#!/usr/bin/perl --
#
# $Id$
#
# Command line options are set of interface and command, they are
# separated by ",".
# The command is launched with parameters:
# -s status
# Linkup: TRUE
# Linkdown: FALSE
# -e ESSID
#
#
#
# New BSD License
#
# Copyright (c) 2011 Hiroaki Abe <hiroaki0404@gmail.com>
# All rights reserved.
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
use strict;
use FileHandle;
use IPC::Open2;
use POSIX qw(SIGALRM);
my $if;
my $cmd;
my $arg;
my %launch;
foreach $arg(@ARGV) {
($if, $cmd) = split(/,/, $arg);
$launch{"$if"} = "$cmd";
}
my $pid = open2(*Reader, *Writer, "scutil");
POSIX::sigaction(SIGALRM,
POSIX::SigAction->new(sub { print Writer "n.cancel\n" }))
or die "Error setting SIGALRM handler: $!\n";
foreach $if(keys(%launch)) {
print Writer "n.add State:/Network/Interface/$if/Link\n";
}
print Writer "n.watch\n";
while(<Reader>){
foreach $if(keys(%launch)) {
if (/$if/){
my $strStatus = `echo "show State:/Network/Interface/$if/Link" | scutil | awk '/Active/{print $3;}'`;
my $ESSID = `echo "show State:/Network/Interface/$if/AirPort" | scutil | awk '/SSID_STR/{print $3;exit;}'`;
system($launch{"$if"} "-s" "$strStatus" "-e" "$ESSID");
}
}
}
print Writer "n.cancel\n";
close Writer;
close Reader;
__END__