a我考网

 找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 132|回复: 2

[LPI认证] Linux系统Perl精简版netstat

[复制链接]
发表于 2012-8-4 12:13:19 | 显示全部楼层 |阅读模式
写作意图:
, [- t$ F0 f, R) s$ I  Linux系统自带的netstat在很多时候抓信息会发现耗CPU很高,特别是在做一些监控系统的时候,发现如果使用了netstat,监控系统本身会造成系统负载比较高,因此试着用perl写下,time测试比自带的netstat更快更节省资源,青年人网提示欢迎批评,呵呵。: c+ G" o( w8 ~  o* O
  #!/usr/bin/perl
7 [+ @. _$ s& n6 k7 t  # Written by FinalBSD at 2008-11-20.
9 d$ U& x0 Q/ B( O! z. u0 a  # The netstat original written by C
6 ]. r6 o0 h3 I! q  # is expensive to me, so this born.
3 r  k( f4 I( W8 d7 b  # As long as you retain this notice you! W: e, ~) o  `2 q% {* N
  # can do whatever you want with this stuff.8 r- N4 x- l4 j
  # If we meet some day, and you think0 Q7 ?8 Q' ^6 _- f
  # this stuff is worth it, you can; m6 b6 g+ {2 F5 _5 {
  # buy me a beer in return.; G: R7 }" T: b" g' P
  use strict;
* z8 c5 D7 P7 g( i" U  use warnings;
6 L9 f3 q' h: }2 X0 t! D  use Getopt::Std;
8 y# j: @# H# m8 a7 `" c. M  my $tcp = "/proc/net/tcp";9 c1 r7 f2 Y7 p
  my $tcp6 = "/proc/net/tcp6";
! g. W8 m0 o6 r: U7 X% V& E5 `+ |1 L& Y  my $route = "/proc/net/route";
0 n9 q; K# w7 x8 n7 p  J$ U  my @tcpState = (
& ?! C2 w& {( }! m8 ]8 f  "NULL",% X$ _7 h* r6 l. E  ^
  "ESTABLISHED" ,- M) J- f6 g1 T% y2 P0 o$ }3 c
  "SYN_SENT",4 G6 m( r8 _) q) u1 b8 }- l
  "SYN_RECV",
) m& ^  [' Q, E7 P% U& h! `" x  "FIN_WAIT1",
2 Z# C0 @7 B: v1 f/ L  "FIN_WAIT2",
, X" y3 V5 A; K6 ?  "TIME_WAIT",
5 H: K( K! t0 n4 T5 n# l/ n# B  "CLOSE",
3 }4 w4 a( E  ?/ D* A  "CLOSE_WAIT",
7 I1 Y& h* u4 P/ B  n0 O' F' N  "LAST_ACK",
, F- Z$ V) W+ {$ P) H5 n' _2 O  "LISTEN",
' z) R* ~' @5 S3 F. f" i  "CLOSING"
0 C) ?4 O/ {" w, i  o  );$ t. z) H8 ?+ E2 ~  |/ l
  my @routeType = (
* h% u( H8 l! a" A  "NULL",7 x8 j2 ^2 Y( p% |. h
  "U", # UP
8 W5 `  [$ s1 E/ P  c, W" t  "G", # Gateway- `# [' z+ b, S( f: z2 O! I$ `# V
  "UG" # UP & Gateway
, d& n6 ?! H: x/ m" N) R  );
回复

使用道具 举报

 楼主| 发表于 2012-8-4 12:13:20 | 显示全部楼层

Linux系统Perl精简版netstat

  my %opts;( ~: m# c3 L* L( t: V& ]6 K
  getopts('nhatsrl', \%opts);
: T1 o2 S5 T# O  F; U" S& f  if($opts{h}) {
& B, m& x" G' ]3 Y; p  O6 F  &usage();* U$ {. [" }4 D. d
  }$ Z) {, D) [$ l
  if($opts{r}) {3 ]$ ]9 {7 i+ Z) t
  &route_info($route);8 ^  `9 k  g( k! C2 p
  exit;
; o$ i, x$ b2 S9 D6 y4 Q7 w  }; W# h3 C8 n: m
  &tcp_info($tcp);
; z6 X) Y  J+ V, l  &tcp_info($tcp6);5 q/ j! Y. J. r. v- h6 z8 [4 l: M" ^9 H$ _
  ######## FUNCTION DEFINITION ######, t( A3 T3 F8 T' j/ j# [3 _; J" ?5 _
  #############################
1 ^- W6 A6 q6 c( d  # Get statistics Information
6 y% T) _1 `4 ^  ############################
3 A0 v0 M8 v: C3 O  ^- e  sub tcp_info($) {4 j7 i( P5 L2 v( U. w
  my $file = shift;
& E6 c8 }( `8 q( ~5 J  open(FH, $file) or die("$!");1 ^7 {1 R4 Y2 x/ c: W. s; K) r4 a
  my $format = "%-30s %-30s %-10s\n";- u. E0 I4 Y9 R8 x( w
  printf($format," Local Address"," Foreign Address","State");
' r) p7 h4 w4 o- v  while() {! b$ f; y) ]. ~  p: n
  next if /local_address/;
1 p6 q7 P' z# H, D& ?4 h3 D  my @data = split;
9 m6 m( K5 e$ O% e  my $state = $tcpState[hex($data[3])];
% s( C. L8 g: I  }% D  # Show listening Sockets.9 }* f. u. p6 x% H( r# m4 w1 ~
  if($opts{l}){
4 u) d) `, k4 I6 O+ k  |  I$ L  printf($format, &hextoint($data[1]), &hextoint($data[2]), $state) if $state =~ /LIST/;- M7 V: ]) y  }6 n7 @
  } else {( M7 H8 r. A2 v5 c" G
  printf($format, &hextoint($data[1]), &hextoint($data[2]), $state);7 q$ a: `* X/ V( Y9 |5 m" [
  }9 s" z+ }+ v0 E* y2 l) h5 n: `
  }
" H6 S9 r  H* O$ ], F) P% J  close(FH);
' p: [# D  N6 a) d# h7 m( O  }7 v) Q3 Z( \+ R2 n# D
  #############################
  t$ B. p1 x4 t. P! Y# I3 T2 ?  # Convert hex to int
+ k3 ]9 t0 z7 q9 m* i+ W! M# z  ############################
0 ^9 G; t! J9 Q9 E! r! |1 J  sub hextoint($) {
7 Q2 h; Z; V( Z' a. \) z  my $tmp = shift;, u/ [6 u) o% F( _! |" w; I
  my @data;
! h: ?. q9 @0 g8 c  my @ip;
7 E5 u0 g# ~& n1 z3 ?  my $int;6 y& ~( e& }8 g; ~3 p
  # if have ':', then it's addr:port,else just port., g$ U- K6 T1 N2 J# ^
  if( $tmp =~ /:/ ) {
- s+ j4 ?8 w8 |! [* X9 y4 O  if( $tmp =~ /FFFF/ ) {
/ `8 M! {' X( w! [  @data = split /:/, $tmp;# v* O( x. J* g- i
  # break it and do convert later.
* G% _4 z0 t" F+ ?  @ip = $data[0] =~ /\w{2}/g;/ d6 k' `- q& l4 X5 f/ ]
  foreach my $index (12..15){0 J6 U9 a' ]8 M% t8 [. d' a
  $ip[$index] = hex($ip[$index]);- V6 D! i- i2 J' n3 m" A( X+ R
  }6 E4 O5 y5 l" l/ O
  $int = sprintf("%-5s %d.%d.%d.%d:%d", "tcp6",$ip[15], $ip[14], $ip[13], $ip[12], hex($data[1]));4 U( ^2 J8 X" x: ]
  } else {
" C& f( O5 P2 @  @data = split /:/, $tmp;5 G7 G: N) w! k* G9 B$ G) L4 [( @
  @ip = $data[0] =~ /\w{2}/g;/ g+ g5 O. h. k( O+ M
  foreach my $index (0..3){9 T" {4 e3 {% _2 }) g& ^# E$ o
  $ip[$index] = hex($ip[$index]);
2 z1 f) Z" w) @! L" y) H0 b  }7 Q2 b* L5 y( C4 a0 J
  $int = sprintf("%-s %d.%d.%d.%d:%d", " ",$ip[3], $ip[2], $ip[1], $ip[0], hex($data[1]));& E1 u' q$ @% w0 U- m) y
  }
回复 支持 反对

使用道具 举报

 楼主| 发表于 2012-8-4 12:13:21 | 显示全部楼层

Linux系统Perl精简版netstat

  } else {
1 j! \& O1 p8 W, q  @ip = $tmp =~ /\w{2}/g;2 O0 v# P  ~3 c" [
  foreach my $index (0..3){3 x: y# u% S% }4 u0 ^
  $ip[$index] = hex($ip[$index]);
$ J" ^9 E7 j$ T) b8 `* n  }
$ Y. b5 s' r; D* A/ R  $int = sprintf("%d.%d.%d.%d", hex($ip[3]), hex($ip[2]), hex($ip[1]), hex($ip[0]));5 [0 h3 A6 a2 ]
  }
, d% r( K; d2 l. d& T+ F& P  return $int;, N$ w% B; r  L5 y8 ?( @
  }
5 W# k. d& h# v4 H% }* e. @% s+ `  ############################## {1 D# {3 ^8 D5 f- M; z+ F- b
  # Show Kernel route table
* p  ?& U# S9 b  z  ############################
. h) A' o' V, ]: H7 t8 A# e. }9 h  sub route_info($) {
9 Q5 e9 y! j( H  my $routefile = shift;( P1 ?# z- {6 e1 o8 l/ e
  open(ROUTE, $routefile) or die ("Can't open route file!\n");" @, ]; x/ O5 I* K. L) n
  my $header = "Iface Destination Gateway Flags Genmask\n";
; H$ V% {+ Q# m/ f  printf("%s", $header);- f1 Z- h/ g! `5 p! @2 ~
  my $format = "%-5s %-15s %-15s %-5s %-15s\n";
9 P9 \) Y: x' a4 X( b: A! x, t  while() {
# T8 s3 p5 n' m: k4 C: l8 v& K. g  next if /Iface/;
9 w, {# e+ i8 U  my @line = split;  e# x9 E" n& I5 N: l" R" h- n9 l
  my ($iface, $dest, $gw, $flags, $mask ) = ($line[0], &hextoint($line[1]), &hextoint($line[2]), hex($line[3]), &hextoint($line[7]));
, D7 T, m3 z6 E% i: j/ n  c  printf($format, $iface, $dest, $gw, $routeType[$flags], $mask );$ Z0 o8 l2 v; u9 T
  }
# [2 J1 X3 {2 s  I  close(ROUTE);
' }7 @3 J/ `" t2 {, `  }7 t6 L) r% n( A! J. e
  #############################' j! ]0 y4 d% z6 _) b
  # Show help information8 G5 |6 ]$ S# d& Z, ^
  ############################* x. `" B; T# P2 ~: u  l! {7 Y: r
  sub usage {) \% H% z$ z. J! y3 l. R
  printf("%s\n\n%-30s\n%-30s\n%-30s\n%-30s\n%-30s\n%-30s\n",. u  M" K- l0 n: ~" d, l* c
  "netstat written in Perl by FinalBSD.Copyright(c) 2008.",0 g5 j* T5 h* D+ D4 ^
  "-n Show numeric ip and port address.",- j3 c9 |; D- X6 D# H1 O
  "-r Display the kernel routing table."," k, z" d0 J0 s
  "-a Show both listening and non-listening socktes.",
; T" o. `, d) Z2 H/ y1 v( v  "-t Show only TCP statistics.",. X" A0 M$ H6 b3 T% B7 `
  "-l Show only listening sockets.",5 \) }: ]- X1 a* p, e0 y5 d
  "-h Show help.",
( R) v$ C' k- e" O7 F  );
, e6 }9 K& B7 M  exit;& l1 A1 `2 H; J$ i& a
  }
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|Woexam.Com ( 湘ICP备18023104号 )

GMT+8, 2024-5-15 16:14 , Processed in 0.359728 second(s), 25 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表