Changeset 383 for trunk


Ignore:
Timestamp:
Jan 4, 2018, 5:13:40 PM (6 years ago)
Author:
g7moreau
Message:
  • Begin addentrydb command
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/klask

    r382 r383  
    7676   'searchdb'             => \&cmd_searchdb,
    7777   'removedb'             => \&cmd_removedb,
     78   'addentrydb'           => \&cmd_addentrydb,
    7879   'cleandb'              => \&cmd_cleandb,
    7980   'search'               => \&cmd_search,
     
    15651566
    15661567#---------------------------------------------------------------
     1568sub cmd_addentrydb {
     1569   my @ARGV = @_;
     1570
     1571   my ($ip, $mac, $network);
     1572
     1573   GetOptions(
     1574      'ip|i=s'      => \$ip,
     1575      'mac|r=s'     => \$mac,
     1576      'network|r=s' => \$net,
     1577      );
     1578
     1579   test_maindb_environnement();
     1580
     1581   my $mac = normalize_mac_address($mac);
     1582
     1583   if ($ip !~ m/^ $RE_IPv4_ADDRESS $/xms or $mac !~ m/^ $RE_MAC_ADDRESS $/xms or not exists $KLASK_CFG->{'network'}{$net}) {
     1584      print {*STDERR} "Error: parameter not valid --ip $ip\n"       if $ip !~ m/^ $RE_IPv4_ADDRESS $/xms;
     1585      print {*STDERR} "Error: parameter not valid --mac $mac\n"     if $mac !~ m/^ $RE_MAC_ADDRESS $/xms;
     1586      print {*STDERR} "Error: parameter not valid --network $net\n" if not exists $KLASK_CFG->{'network'}{$net};
     1587      print {*STDERR} "\n";
     1588      $CMD_DB{'help'}->();
     1589      exit 1;
     1590      }
     1591
     1592   my %resol_arp = resolve_ip_arp_host($ip); # Get hostname_fq ipv4_address
     1593
     1594   if ($resol_arp{'hostname_fq'} eq 'unknow') {
     1595      print {*STDERR} "Error: DNS name not defined for IP $ip (mandatory)\n";
     1596      exit 1;
     1597      }
     1598
     1599   if ($resol_arp{'ipv4_address'} ne $ip) {
     1600      print {*STDERR} "Error: IP DNS resolution strange between $ip and $resol_arp{'ipv4_address'}\n";
     1601      exit 1;
     1602      }
     1603
     1604   my $computerdb = computerdb_load();
     1605   my $timestamp  = time;
     1606
     1607   LOOP_ON_COMPUTER:
     1608   for my $current_ip (keys %{$computerdb}) {
     1609      if ($current_ip eq $ip) {
     1610         print {*STDERR} "Error: IP $ip already exists in the computer database\n";
     1611         exit 1;
     1612         }
     1613
     1614      if ($computerdb->{$current_ip}{'mac_address'} eq $mac) {
     1615         $timestamp = $computerdb->{$current_ip}{'timestamp'} if $computerdb->{$current_ip}{'timestamp'} < $timestamp;
     1616         }
     1617      }
     1618
     1619   $timestamp--; # Just decrement one second to be oldier
     1620
     1621   $computerdb->{$ip} = {
     1622      hostname_fq        => $resol_arp{'hostname_fq'},
     1623      mac_address        => $mac,
     1624      switch_hostname    => 'Unknow',
     1625      switch_description => 'Unknow',
     1626      switch_port_id     => 0,
     1627      switch_port_hr     => 0,
     1628      timestamp          => $timestamp,
     1629      network            => $net,
     1630      };
     1631
     1632   my $dirdb = $KLASK_DB_FILE;
     1633      $dirdb =~ s{ / [^/]* $}{}xms;
     1634   mkdir "$dirdb", 0755 unless -d "$dirdb";
     1635   YAML::Syck::DumpFile("$KLASK_DB_FILE", $computerdb);
     1636   return;
     1637   }
     1638
     1639#---------------------------------------------------------------
    15671640sub cmd_cleandb {
    15681641   my @ARGV = @_;
Note: See TracChangeset for help on using the changeset viewer.