#!/usr/bin/perl -w $tot_in = "0"; $tot_out = "0"; $rule = shift || die "No rule number!"; $type = lc(shift) || die "Need search type!"; $regex = lc(shift) || die "Need a string prefix!"; $other = shift || " "; if ( $rule =~ /^(\d+)$/) { $rule = $1;} else { die "Unmatched rule number!"; } foreach $line (`/sbin/ipfw show $rule`) { chomp($line); LP: if (substr($line,0,1) eq "0") { $line = substr($line,1); goto LP; } LP2: $line =~ s/ / /g; if (index($line," ") > -1) { $line =~ s/ / /g; goto LP2; } @args = split / /, $line; if ($args[0] eq $rule) { if (substr($type,0,1) eq "a") { if (index($line,"count ip from any to ".$regex) > -1) { $tot_in = $args[2]; next; } if (index($line,"count ip from ".$regex." to any") > -1) { $tot_out = $args[2]; next; } } elsif (substr($type,0,1) eq "u") { if (index($line," uid ".$regex." in") > -1) { $tot_in = $args[2]; next; } if (index($line," uid ".$regex." out") > -1) { $tot_out = $args[2]; next; } } elsif (substr($type,0,1) eq "i") { if (index($line," ".$regex." from any to any in") > -1) { $tot_in = $args[2]; next; } if (index($line," ".$regex." from any to any out") > -1) { $tot_out = $args[2]; next; } } elsif (substr($type,0,1) eq "d") { if (index($line,$regex." in recv ".$other) > -1) { $tot_in = $args[2]; next; } if (index($line,$regex." out xmit ".$other) > -1) { $tot_out = $args[2]; next; } } } } $boottime = `/sbin/sysctl -a kern.boottime`; my @vargs = split / /, $boottime; $boottime = substr($vargs[4],0,-1); $current = time(); $totalticks = $current - $boottime; $time = convert_time($totalticks); print $tot_in."\n"; print $tot_out."\n"; print $time."\n"; print `hostname`; sub convert_time { my $sec = 0; my $min = 0; my $hr = 0; my $day = 0; my $secc = ""; my $minn = ""; my $hrr = ""; my $dayy = ""; my $tmp = ""; my $ticks = $_[0]; my $tick; if ($ticks < 60) { return($ticks." seconds"); } LP: $day = ($ticks / 60 / 60 / 24); if (index($day,".") > -1) { ($day) = split /\./, $day; } $hr = ($ticks - (86400 * $day)) / 60 / 60; if (index($hr,".") > -1) { ($hr) = split /\./, $hr; } $min = (($ticks - (86400 * $day) - (3600 * $hr)) / 60); if (index($min,".") > -1) { ($min) = split /\./, $min; } $sec = ($ticks - (86400 * $day) - (3600 * $hr) - (60 * $min)); if (index($sec,".") > -1) { ($sec) = split /\./, $sec; } if ($sec > 1) { $secc = "s"; } if ($min > 1) { $minn = "s"; } if ($hr > 1) { $hrr = "s"; } if ($day > 1) { $dayy = "s"; } if ($day > 0) { $tmp = $day." day".$dayy; } if ($hr > 0) { if (length($tmp) > 0) { $tmp = $tmp.", ".$hr." hour".$hrr; } else { $tmp = $hr." hour".$hrr; } } if ($min > 0) { if (length($tmp) > 0) { $tmp = $tmp.", ".$min." min".$minn; } else { $tmp = $min." min".$minn; } } if ($sec > 0) { if (length($tmp) > 0) { $tmp = $tmp.", ".$sec." sec".$secc; } else { $tmp = $sec." sec".$secc; } } return($tmp); }