#!/usr/bin/perl
#
# ifstatus
#
# Get operating status of a network interface
#
# Syntax: ifstatus <interface name>
# eg: ifstatus hme1
#

# Check if we got a parameter: if we did not, bail out
scalar @ARGV == 1 or die "Error: must specify interface name as single parameter";

# Now we check if we got something that *looks* like an interface name,
# that is, it is formed by two to four letters followed by a decimal number
$ARGV[0] =~ /^([a-z]{2,4})(\d+)$/
  or die "Error: I can not understand that interface name";

# Now we should have interface class in $1 and instance in $2
my ($class, $instance) = ($1,$2);

# Get current instance value for device class, to be able to leave things
# as we found them
my $oldinstance = readpipe "/sbin/ndd /dev/$class instance"
  or die "Error reading current instance";

$oldinstance =~ /^\d+$/ or die "Error reading current instance";

# Set instance
system "/sbin/ndd -set /dev/$class instance $instance"
  or die "Could not set new instance";

# Read status
my $status = readpipe "/sbin/ndd /dev/$class link_status"
  or die "Could not read link_status";

# Tell user what we have found out
if ($status)
{
  print "Up";
} else {
  print "Down";
}

# Leave things as before
system "/sbin/ndd -set /dev/$class instance $oldinstance"
  or die "Could not set instance back";
