Changeset 69


Ignore:
Timestamp:
Nov 2, 2010, 4:43:26 PM (13 years ago)
Author:
g7moreau
Message:
  • Add command cmd_ip_free

klask ip-free

return ip not use in one or more VLAN.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/klask

    r68 r69  
    1717use Net::CIDR::Lite;
    1818use NetAddr::IP;
    19 use Getopt::Long;
     19use Getopt::Long qw(GetOptions GetOptionsFromArray);;
    2020use Socket;
    2121
     
    6161   exportsw   => \&cmd_exportsw,
    6262   iplocation => \&cmd_iplocation,
     63   'ip-free'  => \&cmd_ip_free,
    6364   'search-mac-on-switch' => \&cmd_search_mac_on_switch,
    6465   );
     
    585586
    586587 klask updatedb
    587  klask exportdb
     588 klask exportdb --format [txt|html]
    588589
    589590 klask updatesw
    590  klask exportsw
     591 klask exportsw --format [txt|dot]
    591592
    592593 klask searchdb computer
    593594 klask search   computer
    594595 klask search-mac-on-switch switch mac_addr
     596
     597 klask ip-free --day number_of_day [vlan_name]
    595598
    596599 klask enable  switch port
     
    11411144   }
    11421145
     1146sub cmd_ip_free {
     1147   my @vlan_name = @_;
     1148
     1149   my $days_to_dead = 365 * 2;
     1150   my $format = 'txt';
     1151
     1152   my $ret = GetOptionsFromArray(\@vlan_name,
     1153      'day|d=i'      => \$days_to_dead,
     1154      'format|f=s'   => \$format,
     1155      );
     1156
     1157   @vlan_name = get_list_network() if not @vlan_name;
     1158
     1159#   my %possible_format = (
     1160#      txt  => \&cmd_exportdb_txt,
     1161#      html => \&cmd_exportdb_html,
     1162#      );
     1163#
     1164#   $format = 'txt' if not defined $possible_format{$format};
     1165#
     1166#   $possible_format{$format}->(@ARGV);
     1167
     1168   my $computerdb = {};
     1169      $computerdb = YAML::Syck::LoadFile("$KLASK_DB_FILE") if -e "$KLASK_DB_FILE";
     1170   my $timestamp = time;
     1171
     1172   my $timestamp_barrier = $timestamp - (3600 * 24 * $days_to_dead );
     1173
     1174   my %result_ip= ();
     1175
     1176   ALL_NETWORK:
     1177   for my $vlan (@vlan_name) {
     1178
     1179      my @ip_list = get_list_ip($vlan);
     1180#      my $current_interface = get_current_interface($vlan);
     1181     
     1182      LOOP_ON_IP_ADDRESS:
     1183      for my $ip (@ip_list) {
     1184
     1185         next LOOP_ON_IP_ADDRESS if
     1186            exists $computerdb->{$ip}
     1187            && $computerdb->{$ip}{timestamp} > $timestamp_barrier;
     1188
     1189         my $ip_date_last_detection = '';
     1190         if (exists $computerdb->{$ip}) {
     1191            my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime $computerdb->{$ip}{timestamp};
     1192            $year += 1900;
     1193            $mon++;
     1194            $ip_date_last_detection = sprintf '%04i-%02i-%02i %02i:%02i', $year, $mon, $mday, $hour, $min;
     1195            }
     1196
     1197         $result_ip{$ip} ||= {};
     1198         $result_ip{$ip}->{date_last_detection} = $ip_date_last_detection;
     1199
     1200         my $packed_ip = scalar gethostbyname($ip);
     1201         my $hostname_fq = 'unknown';
     1202            $hostname_fq = scalar gethostbyaddr($packed_ip, AF_INET) || 'unknown' if defined $packed_ip;
     1203         $result_ip{$ip}->{hostname_fq} = $hostname_fq;
     1204         
     1205         $result_ip{$ip}->{vlan} = $vlan;
     1206         }
     1207      }
     1208
     1209   printf "%-15s %-40s %-16s %s\n", qw(IPv4-Address Hostname-FQ Date VLAN);
     1210   print "-------------------------------------------------------------------------------\n";
     1211   LOOP_ON_RESULT:
     1212   foreach my $ip (Net::Netmask::sort_by_ip_address(keys %result_ip)) {
     1213         printf "%-15s %-40s %-16s %s\n", $ip, $result_ip{$ip}->{hostname_fq}, $result_ip{$ip}->{date_last_detection}, $result_ip{$ip}->{vlan};
     1214      }
     1215   }
     1216
    11431217sub cmd_enable {
    11441218   my $switch = shift;
     
    15641638 klask searchdb computer
    15651639 klask search   computer
     1640 klask search-mac-on-switch switch mac_addr
     1641
     1642 klask ip-free --day number_of_day [vlan_name]
    15661643
    15671644 klask enable  switch port
Note: See TracChangeset for help on using the changeset viewer.