mac2if.in 890 Bytes
Newer Older
1 2
#!/usr/local/bin/perl -w

Mac Newbold's avatar
Mac Newbold committed
3
if ($#ARGV < 0) {die("Usage: mac2if <mac|if> <mac|if> <...>\n");}
4 5 6

use Mysql;

7
my $dbh = Mysql->connect("localhost","@TBDBNAME@","script","none");
8 9 10 11

my @list = "";
my $n=0;
foreach my $mac ( @ARGV ) { 
12 13 14
  if ( $mac =~ /^(sh\d+)(-\d)?(:\d)?$/ ) {
    my $node = $1. (defined $2 ? $2 : "-" );
    $list[$n] .= "(node_id like '$node%')";
15
  } elsif ( $mac =~ /^([a-zA-Z]+\d+):(\d+)$/ ) {
16
    my $node=$1;
Mac Newbold's avatar
Mac Newbold committed
17 18
    my $if=$2;
    $list[$n] .= "(node_id='$node' and card='$if')";
19
  } elsif ( $mac =~ /^([a-zA-Z]+\d+)$/ ) {
20
    my $node=$1;
Mac Newbold's avatar
Mac Newbold committed
21 22 23 24 25 26 27
    $list[$n] .= "(node_id='$node')";
  } else {
    $mac =~ s/^(\d):/0$1:/;
    $mac =~ s/:(\d):/:0$1:/g;
    $mac =~ s/://g;
    $list[$n] ="MAC='\L$mac'";
  }
28 29 30 31 32
  $n++;
}

my $cond = join(" or ",@list);

33 34
#print "Using conditions '$cond'\n";

35 36 37 38
my $sth = $dbh->
    query("select * from interfaces where $cond");

print $sth->as_string();