#!/usr/bin/perl -- -*-CPerl-*- use strict; $::Prog = "ifup-tryall"; ## @ifs = ifs_up() ## -- Returns list of eth/wlan interfaces currently up sub ifs_up { my (@lines); ## See if any eth interfaces are already up @lines = `/bin/netstat -i`; @lines = (map (($_ =~ /^(eth\d+)\s+/) ? $1 : (), @lines), map (($_ =~ /^(wlan\d+)\s+/) ? $1 : (), @lines)); return (@lines); }; sub main { my ($pid, $if, @ifs, @lines); ($> == 0) || die ("Must run $::Prog as root\n"); ## Detach & fork setpgrp() || die ("$::Prog: Cannot setpgrp: $!"); defined ($pid = fork()) || die ("$::Prog: cannot fork: $!"); $pid && exit(0); ## I am the parent ## Redirect output to log file open (STDIN, "/dev/null"); open (STDOUT, ">>/var/log/$::Prog") || die ("cannot open STDOUT to /var/log/$::Prog: $!"); select (STDOUT); $| = 1; open (STDERR, ">&STDOUT") || die ("cannot open STDERR to STDOUT (dup): $!"); select (STDERR); $| = 1; select (STDOUT); @ifs = ifs_up(); if (scalar (@ifs)) { ## One or more interfaces are already up print ::STDOUT ($_ = localtime, " interfaces already up: ", join (" ", @ifs), "\n"); return; }; ## Get configured interfaces @ifs = ; @ifs = (map (($_ =~ /(eth\d+)$/) ? $1 : (), @ifs), map (($_ =~ /(wlan\d+)$/) ? $1 : (), @ifs)); if (! scalar (@ifs)) { print ::STDOUT ($_ = localtime, " No configured interfaces found\n"); return; }; ## Bring one up foreach $if (@ifs) { print ::STDOUT ($_ = localtime, " Trying to start $if\n"); system ("/sbin/ifup", $if, "up"); if (scalar (ifs_up())) { print ::STDOUT ($_ = localtime, " Interface is now up\n"); return; }; }; print ::STDOUT ($_ = localtime, " Cannot bring up any interfaces, exiting\n"); }; main();