- Timestamp:
- Nov 2, 2010, 4:43:26 PM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/klask
r68 r69 17 17 use Net::CIDR::Lite; 18 18 use NetAddr::IP; 19 use Getopt::Long ;19 use Getopt::Long qw(GetOptions GetOptionsFromArray);; 20 20 use Socket; 21 21 … … 61 61 exportsw => \&cmd_exportsw, 62 62 iplocation => \&cmd_iplocation, 63 'ip-free' => \&cmd_ip_free, 63 64 'search-mac-on-switch' => \&cmd_search_mac_on_switch, 64 65 ); … … 585 586 586 587 klask updatedb 587 klask exportdb 588 klask exportdb --format [txt|html] 588 589 589 590 klask updatesw 590 klask exportsw 591 klask exportsw --format [txt|dot] 591 592 592 593 klask searchdb computer 593 594 klask search computer 594 595 klask search-mac-on-switch switch mac_addr 596 597 klask ip-free --day number_of_day [vlan_name] 595 598 596 599 klask enable switch port … … 1141 1144 } 1142 1145 1146 sub 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 1143 1217 sub cmd_enable { 1144 1218 my $switch = shift; … … 1564 1638 klask searchdb computer 1565 1639 klask search computer 1640 klask search-mac-on-switch switch mac_addr 1641 1642 klask ip-free --day number_of_day [vlan_name] 1566 1643 1567 1644 klask enable switch port
Note: See TracChangeset
for help on using the changeset viewer.