|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
linux系统的文件布置,etc/,opt/目录的内容等;
写作企图:
Linux体系自带的netstat在良多时分抓信息会发明耗CPU很高,出格是在做一些监控体系的时分,发明假如利用了netstat,监控体系自己会形成体系负载对照高,因而试着用perl写下,time测试比自带的netstat更快更节俭资本,接待品评,呵呵。
#!/usr/bin/perl
#WrittenbyFinalBSDat2008-11-20.
#ThenetstatoriginalwrittenbyC
#isexpensivetome,sothisborn.
#Aslongasyouretainthisnoticeyou
#candowhateveryouwantwiththisstuff.
#Ifwemeetsomeday,andyouthink
#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{h}){
&usage();
}
if($opts{r}){
&route_info($route);
exit;
}
&tcp_info($tcp);
&tcp_info($tcp6);
######################FUNCTIONDEFINITION###########################
#############################
#GetstatisticsInformation
############################
subtcp_info($){
my$file=shift;
open(FH,$file)ordie("$!");
my$format="%-30s%-30s%-10s
";
printf($format,"LocalAddress","ForeignAddress","State");
while(<FH>){
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(FH);
}
#############################
#Converthextoint
############################
subhextoint($){
my$tmp=shift;
my@data;
my@ip;
my$int;
#ifhave:,thenitsaddr: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=shift;
open(ROUTE,$routefile)ordie("Cantopenroutefile!
");
my$header="IfaceDestinationGatewayFlagsGenmask
";
printf("%s",$header);
my$format="%-5s%-15s%-15s%-5s%-15s
";
while(<ROUTE>){
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
%-30s
%-30s
%-30s
%-30s
%-30s
%-30s
",
"netstatwritteninPerlbyFinalBSD.Copyright(c)2008.",
"-nShownumericipandportaddress.",
"-rDisplaythekernelroutingtable.",
"-aShowbothlisteningandnon-listeningsocktes.",
"-tShowonlyTCPstatistics.",
"-lShowonlylisteningsockets.",
"-hShowhelp.",
);
exit;
}
</p>
不同版本的Linux命令数量不一样,这里笔者把它们中比较重要的和使用频率最多的命令。 |
|