From c59b49203834348ecfb256350d574f395d84606c Mon Sep 17 00:00:00 2001 From: Alexander Sulfrian Date: Fri, 11 Jan 2013 03:49:26 +0100 Subject: add some more comments --- net_if | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/net_if b/net_if index dead89f..f2f1d31 100755 --- a/net_if +++ b/net_if @@ -17,7 +17,8 @@ my %nagios = ( # Print nagios compatible status message and terminat script # with apropiate return code. # -# parameter: type of message +# parameter: +# type of message sub msg ($@) { my $type = shift; my ($msg, $exit) = @{$nagios{$type}}; @@ -31,25 +32,32 @@ sub msg ($@) { # This resolves interfaces groups, but only return the interfaces # that are up. # -# parameter: list of interfaces +# parameter: +# list of interfaces sub get_if_status (@) { my (@up, @down, @error); while (@_) { + # untaint interface name my $if = shift @_; next unless ($if =~ m/^([-\w]+)$/); $if = $1; + # We are doing an two step process. First we do a fork and + # after that we exec the ifconfig child process. This is + # necessary to pipe stderr of the child process away, while + # using the list syntax for open and exec. my $pid = open(IFCONFIG, "-|"); msg('crit', "fork: $!") unless defined $pid; if ($pid == 0) { - # child + # redirect stderr open(STDERR, ">", "/dev/null"); exec("ifconfig", $if) } while () { + # parse ifconfig output: if ($_ =~ m/^([-a-v0-9]+): flags=([0-9]+)