perl程序显示本机的tcp连接数例子
- #!/usr/bin/perl
- my $_cmd="netstat -an|grep -v unix";
- my @_output=`$_cmd`;
- my $_estab = 0;
- my $_listen = 0;
- my $_timewait = 0;
- my $_timeclose = 0;
- my $_finwait1 = 0;
- my $_finwait2 = 0;
- my $_synsent = 0;
- my $_synrecv = 0;
- my $_closewait = 0;
- foreach (@_output) {
- $_estab++ if /ESTABLISHED/;
- $_listen++ if /LISTEN/;
- $_timewait++ if /TIME_WAIT/;
- $_timeclose++ if /TIME_CLOSE/;
- $_finwait1++ if /FIN_WAIT1/;
- $_finwait2++ if /FIN_WAIT2/;
- $_synsent++ if /SYN_SENT/;
- $_synrecv++ if /SYN_RECV/;
- $_closewait++ if /CLOSE_WAIT/;
- }
- print " established:$_estab\n listen:$_listen\n timewait:$_timewait\n timeclose:$_timeclose\n finwait1:$_finwait1\n finwait2:$_finwait2\n synsent:$_synsent\n synrecv:$_synrecv\n closewait:$_closewait\n";
运行结果如下
以上运行结果等效于
netstat -an|awk '/^tcp/{++A[$NF]}END{for(a in A) print a,A[a]}'
本文转自it你好 51CTO博客,原文链接:http://blog.51cto.com/itnihao/1011578,如需转载请自行联系原作者