&tcp_info($tcp6);
5 S# ]+ P1 L% A( r8 K6 B ######## FUNCTION DEFINITION ######6 e: Z1 U! f1 S- l" v- f9 |" d4 g
#############################
- @/ y0 P4 Y% `5 J/ T # Get statistics Information* v' o* U. Z9 ? _& \; o
############################0 X. v1 f+ U- M: P/ V n" x
sub tcp_info($) {4 _; a8 H& B* N! k- J# k
my $file = shift;; h* S- J& S: m1 S" E0 E. i
open(FH, $file) or die("$!");, Q; r" b3 J* w% y
my $format = "%-30s %-30s %-10sn";) A& K& r" j2 g+ ] x1 ]) u' L
printf($format," Local Address"," Foreign Address","State");6 J5 N9 T$ t( p
while() {
9 g# O, P4 G6 g9 T( M- i7 H next if /local_address/;
I8 S/ p! J* N% o9 p my @data = http://www.qnr.cn/pc/lin/lpi/fudao/200811/split;
$ f% ]9 C# r/ Z, _% A6 ^ my $state = $tcpState[hex($data[3])];! R6 ^+ s5 g S5 W) w+ G+ P
# Show listening Sockets.
5 Y, h) C: E( S9 R if($opts{l}){
* l# d, C$ j4 z) D. f" S printf($format, &hextoint($data[1]), &hextoint($data[2]), $state) if $state =~ /LIST/;! H9 S% V w7 _; H7 E0 p B% J
} else {
1 v0 n- n# e9 X printf($format, &hextoint($data[1]), &hextoint($data[2]), $state);
" \9 r8 `# W" {" d6 I8 ] }4 X+ E' c5 c0 I0 U; L
}, h% [0 \7 y2 s( ]0 J
close(FH);
3 y! A- x4 g, o+ @% ? }
; a8 h) |7 |9 [ #############################
" S0 e- G6 p+ L! R0 R- f # Convert hex to int
* ]' g" z. Y1 K2 m2 }! M ^ ############################
0 w3 c w; F5 I3 N# }6 [7 j sub hextoint($) { ~/ Y$ X/ v0 Z
my $tmp = shift;
1 o% R1 g6 r) F my @data;' Z! Z8 y9 }" u) g9 z
my @ip;
; O* [. _# A- o% s5 c8 Z my $int;
9 h8 L4 _/ k: t9 p! E- J # if have ':', then it's addr:port,else just port.
( [& C% Q+ V; Q' I( o6 ^9 f if( $tmp =~ /:/ ) {
% |3 R! ?; {4 M2 e" q if( $tmp =~ /FFFF/ ) {" y! I8 h* n: m/ S( [
@data = http://www.qnr.cn/pc/lin/lpi/fudao/200811/split /:/, $tmp;
2 j) `0 w J2 E! q3 Q # break it and do convert later.$ h/ [* u* j$ ?8 |8 A2 r/ Y
@ip = $data[0] =~ /w{2}/g;7 z' M4 K% O. n) p0 p7 D+ k
foreach my $index (12..15){( V6 `# ~1 w& b: E
$ip[$index] = hex($ip[$index]);* q, ?# }: v Y1 { \
}/ J; |' g, w* E* s2 d
$int = sprintf("%-5s %d.%d.%d.%d:%d", "tcp6",$ip[15], $ip[14], $ip[13], $ip[12], hex($data[1]));
! p1 V3 L: P% }4 @" q } else {1 f. `8 _" N" Q u. r
@data = http://www.qnr.cn/pc/lin/lpi/fudao/200811/split /:/, $tmp;! W. `! }+ a6 U2 K+ [- X0 m
@ip = $data[0] =~ /w{2}/g;
, u; C9 r/ i5 Q. {+ S' t, B/ W foreach my $index (0..3){# f7 e! v: a" T$ R2 ?4 D' w
$ip[$index] = hex($ip[$index]);: S {+ F- k- f% T6 W
}
" Y8 b, y: {% Q7 s, }% z/ X6 ? $int = sprintf("%-s %d.%d.%d.%d:%d", " ",$ip[3], $ip[2], $ip[1], $ip[0], hex($data[1]));0 m& N4 |& a: E V: O
}7 S& h) ^- X+ w9 |' G
} else {
7 P) U5 ?: t1 O" G/ i; G- M2 X! o @ip = $tmp =~ /w{2}/g;
( r# }3 {4 J! H: w, l* _; o; J' @ foreach my $index (0..3){
4 @/ A4 s( a4 a$ @' S $ip[$index] = hex($ip[$index]);
2 ]0 N" u$ Y, d9 e! e( I }
0 r# K/ K" c$ H $int = sprintf("%d.%d.%d.%d", hex($ip[3]), hex($ip[2]), hex($ip[1]), hex($ip[0]));
- x/ F) w+ y* P6 m! }* H }
6 ?0 e k& a4 [ return $int;
, k, [! {! l4 S+ Z' }! M3 g: w }% e0 L7 e( r$ O6 O* q. d$ Q. E
#############################" e0 x. Q' D( X* _
# Show Kernel route table0 M6 }* h2 m1 c0 e& t3 t2 A
############################. y, `4 ^! {$ B" V* Y
sub route_info($) {7 k$ M# X! c0 p# ]
my $routefile = shift;% n1 B- Y1 q% X& o2 E4 |1 t/ j
open(ROUTE, $routefile) or die ("Can't open route file!n");+ S- ?' G4 f0 ^6 ]
my $header = "Iface Destination Gateway Flags Genmaskn";& r3 d( ?! B. ]2 _- B
printf("%s", $header);, @% K3 L9 j _/ j- J( J
my $format = "%-5s %-15s %-15s %-5s %-15sn";
, v6 J# f; W$ `+ r5 n1 L2 @$ y while() {
! E6 C# }2 @. f: P& [ next if /Iface/;
/ J# L/ i# {! b: u0 q: X$ p my @line = split;: m, y) j0 {+ L3 e# x- m
my ($iface, $dest, $gw, $flags, $mask ) = ($line[0], &hextoint($line[1]), &hextoint($line[2]), hex($line[3]), &hextoint($line[7]));
6 S1 Z$ |5 s6 h/ N( g printf($format, $iface, $dest, $gw, $routeType[$flags], $mask );
- [3 Q$ T1 L, k& ^3 \; c' k% ?+ | }: |# R* S/ R' j6 S7 b
close(ROUTE);
: V9 d' f! q! w6 D) q }: d7 r4 X- C x4 p' o
#############################! P1 h2 r# L+ R0 M: ~; `' g
# Show help information
0 j# Z U" E( [& F' T ############################8 N! c# E+ z& e# L
sub usage {
% L9 h6 I d8 |% L- l printf("%snn%-30sn%-30sn%-30sn%-30sn%-30sn%-30sn",
5 @6 i9 F1 L! C9 z( o "netstat written in Perl by FinalBSD.Copyright(c) 2008.",
1 e2 A8 U+ J d0 {. O "-n Show numeric ip and port address.",8 \% J- O( f7 b6 f! B0 i
"-r Display the kernel routing table.",0 A6 K9 o/ X2 a$ s8 ]$ c& w: N3 ]7 ]- h2 V
"-a Show both listening and non-listening socktes.",$ _2 P& a( J7 ^1 |- k) Y
"-t Show only TCP statistics.",
/ v) K: E" x, M; O$ O0 t; m "-l Show only listening sockets.",, s/ _. O0 X+ C0 J
"-h Show help.",3 g+ `1 W! ~2 I; q& W
);
* I C" M, w+ `* c; e* v0 P3 {! E! r- z exit;4 K. v5 E9 ?" |7 V
} |