2011/01/23

Script: Test whether network is alive


#!/usr/bin/perl

use strict;
use warnings;
use threads;
use threads::shared;


# shared variable.

my $alive = 0;
share($alive);

# ping Google.
my $thr = threads->create('Ping_Google');
$thr->detach();
sleep(3);

# restart wireless network if down.

system('/etc/init.d/wireless_network restart') if not $alive;


# sub-routines.

sub Ping_Google
{
    for (`ping -c 1 -W 1 www.google.com.tw 2> /dev/null`) {
        $alive = 1 if /0% packet loss/o;
        last if $alive;
    }
}