Writing intention: Linux system with netstat in a lot of time to grab information will find CPU consumption is very high, especially in doing some monitoring system time, Discovery if you use the netstat, monitoring system itself will cause the system load is high, so try to use perl to write down the time test than with netstat faster and save resources, welcome criticism, huh, huh.
#!/usr/bin/perl#WrittenbyFinalBSDat2008-11-20.#ThenetstatoriginalwrittenbyC#isexpensivetome,sothisborn.#Aslongasyouretainthisnoticeyou#candowhateveryouwantwiththisstuf.#Ifwemeetsomeday,andyoutink#thisstuffisworthit,youcan#buymeabeerinreturn.usestrict;usewarnings;useGetopt::Std;my$tcp="/proc/net/tcp";my$tcp6="/proc/net/tcp6";my$route="/proc/net/route";my @tcpState =("NULL","ESTABLISHED","SYN_SENT","SYN_RECV","FIN_WAIT1","FIN_WAIT2","TIME_WAIT","CLOSE","CLOSE_WAIT","LAST_ACK","LISTEN","CLOSING");my @routeType =("NULL","U",#UP"G",#Gateway"UG"#UP&Gateway);my%opts;getopts('nhatsrl',\%opts);if($opts{}){&usage();} if($opts{r}){&route_info($route);exit;} &tcp_info($tcp);&tcp_info($tcp6);######################FUNCTIONDEFINITION#########################################################GetstatisticsInformation############################subtcp_info($){my$file=sit;open(FH,$file)ordie("$!"); my$format="%-30s%-30s%-10s\n";printf($format,"LocalAddress","ForeignAddress","State");while(
){nextif/local_address/;my @data =split;my$state=$tcpState[hex($data[3])];#ShowlisteningSockets.if($opts{l}){printf($format,&hextoint($data[1]),&hextoint($data[2]),$state)if$state=~/LIST/;} else{printf($format,&hextoint($data[1]),&hextoint($data[2]),$state);}} close(H);} ##############################Converthextoint############################subhextoint($){my$tmp=sit;my @data; my @ip; my$int;#ifhave':',thenit'saddr:port,elsejustport.if($tmp=~/:/){if($tmp=~/FFFF/){ @data =split/:/,$tmp;#breakitanddoconvertlater. @ip =$data[0]=~/\w{2}/g;foreachmy$index(12..15){$ip[$index]=hex($ip[$index]);} $int=sprintf("%-5s%d.%d.%d.%d:%d","tcp6",$ip[15],$ip[14],$ip[13],$ip[12],hex($data[1]));} else{ @data =split/:/,$tmp; @ip =$data[0]=~/\w{2}/g;foreachmy$index(0..3){$ip[$index]=hex($ip[$index]);} $int=sprintf("%-s%d.%d.%d.%d:%d","",$ip[3],$ip[2],$ip[1],$ip[0],hex($data[1]));}} else{ @ip =$tmp=~/\w{2}/g;foreachmy$index(0..3){$ip[$index]=hex($ip[$index]);} $int=sprintf("%d.%d.%d.%d",hex($ip[3]),hex($ip[2]),hex($ip[1]),hex($ip[0]));} return$int;} ##############################ShowKernelroutetable############################subroute_info($){my$routefile=sit;open(ROUTE,$routefile)ordie("Can'topenroutefile!\n");my$header="IfaceDestinationGatewayFlagsGenmask\n";printf("%s",$header);my$format="%-5s%-15s%-15s%-5s%-15s\n";while(
){nextif/Iface/;my @line =split;my($iface,$dest,$gw,$flags,$mask)=($line[0],&hextoint($line[1]),&hextoint($line[2]),hex($line[3]),&hextoint($line[7]));printf($format,$iface,$dest,$gw,$routeType[$flags],$mask);} close(ROUTE);} ##############################Showhelpinformation############################subusage{printf("%s\n\n%-30s\n%-30s\n%-30s\n%-30s\n%-30s\n%-30s\n","netstatwritteninPerlbyFinalBSD.Copyright(c)2008.","-nShownumericipandportaddress.","-rDisplaythekernelroutingtable.","-aShowbothlisteningandnon-listeningsocktes.","-tShowonlyTCPstatistics.","-lShowonlylisteningsockets.","-hShowhelp.",);exit;}
No comments:
Post a Comment