Changeset 136 for trunk/klask


Ignore:
Timestamp:
Mar 31, 2014, 5:48:40 PM (10 years ago)
Author:
g7moreau
Message:
  • Add option to searchdb by mac
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/klask

    r135 r136  
    338338   }
    339339
    340 ###
     340#--------------------------------------------------------------------------------
     341
     342sub normalize_mac_address {
     343   my $mac_address = shift;
     344
     345   return join q{:}, map { substr( uc("00$_"), -2) } split m/ [:-] /xms, $mac_address;
     346   }
     347
     348#--------------------------------------------------------------------------------
    341349# convertit l'@ mac en decimal
    342350sub mac_address_hex_to_dec {
     
    695703
    696704sub cmd_searchdb {
     705   my @ARGV  = @_;
     706
     707   my $kind;
     708
     709   my $ret = GetOptions(
     710      'kind'   => \$kind,
     711      );
     712
     713   if
     714
     715   my %possible_search = (
     716      host  => \&cmd_searchdb_host,
     717      mac   => \&cmd_searchdb_mac,
     718      );
     719
     720   $search = 'host' if not defined $possible_search{$kind};
     721
     722   $possible_search{$kind}->(@ARGV);
     723   return;
     724   }
     725
     726
     727sub cmd_searchdb_host {
    697728   my @computer = @_;
    698729
     
    719750         $computerdb->{$ip}{mac_address},
    720751         $date;
     752      }
     753   return;
     754   }
     755
     756sub cmd_searchdb_mac {
     757   my @mac = map { normalize_mac_address($_) } @_;
     758
     759   my $computerdb = YAML::Syck::LoadFile("$KLASK_DB_FILE");
     760
     761   LOOP_ON_MAC:
     762   for my $mac (@mac) {
     763      LOOP_ON_COMPUTER:
     764      for my $ip (keys %{$computerdb}) {
     765         next LOOP_ON_COMPUTER if $mac ne $computerdb->{$ip}{mac_address};
     766 
     767         my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime $computerdb->{$ip}{timestamp};
     768         $year += 1900;
     769         $mon++;
     770         my $date = sprintf '%04i-%02i-%02i %02i:%02i', $year, $mon, $mday, $hour, $min;
     771
     772         printf "%-22s %2s %-30s %-15s %-18s %s\n",
     773            $computerdb->{$ip}{switch_hostname},
     774            $computerdb->{$ip}{switch_port},
     775            $computerdb->{$ip}{hostname_fq},
     776            $ip,
     777            $computerdb->{$ip}{mac_address},
     778            $date;
     779         next LOOP_ON_MAC;
     780         }
     781
    721782      }
    722783   return;
     
    20982159 klask exportsw --format [txt|dot]
    20992160
    2100  klask searchdb computer
     2161 klask searchdb --kind [host|mac] computer [mac-address]
    21012162 klask search   computer
    21022163 klask search-mac-on-switch switch mac_addr
Note: See TracChangeset for help on using the changeset viewer.