Linux Perl Lite version netstat

Source: Internet
Author: User
Article Title: Linux Perl Lite version netstat. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.

Writing intention:

The built-in netstat of Linux system often finds that the CPU usage is high when capturing information, especially when some monitoring systems are used, if netstat is used, the monitoring system itself may cause a high system load. Therefore, you can use perl to write down the log. The time test is faster and more resource-saving than the built-in netstat. Thank you for your criticism.

#! /Usr/bin/perl
# Written by FinalBSD at 2008-11-20.

# The netstat original written by C
# Is expensive to me, so this born.
# As long as you retain this notice you
# Can do whatever you want with this stuff.
# If we meet some day, and you think
# This stuff is worth it, you can
# Buy me a beer in return.
Use strict;
Use warnings;
Use Getopt: 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 );
###################### Function definition ################ ###########
#############################
# Get statistics Information
############################
Sub tcp_info ($ ){
My $ file = shift;
Open (FH, $ file) or die ("$! ");
My $ format = "%-30 s %-30 s %-10s \ n ";
Printf ($ format, "Local Address", "Foreign Address", "State ");
While ( ){
Next if/local_address /;
My @ data = split;
My $ state = $ tcpState [hex ($ data [3])];
# Show listening Sockets.
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 );
}
#############################
# Convert hex to int
############################
Sub hextoint ($ ){
My $ tmp = shift;
My @ data;
My @ ip;
My $ int;
# If have ':', then it's addr: port, else just port.
If ($ tmp = ~ /:/){
If ($ tmp = ~ /FFFF /){
@ Data = split/:/, $ tmp;
# Break it and do convert later.
@ Ip = $ data [0] = ~ /\ W {2}/g;
Foreach my $ index (12 .. 15 ){
$ Ip [$ index] = hex ($ ip [$ index]);
}
$ Int = sprintf ("%-5 s % 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;
Foreach my $ 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;
Foreach my $ 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;
}
#############################
# Show Kernel route table
############################
Sub route_info ($ ){
My $ routefile = shift;
Open (ROUTE, $ routefile) or die ("Can't open route file! \ N ");
My $ header = "Iface Destination Gateway Flags Genmask \ n ";
Printf ("% s", $ header );
My $ format = "%-5 s %-15 s %-15 s %-5 s %-15s \ n ";
While ( ){
Next if/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 );
}
#############################
# Show help information
############################
Sub usage {
Printf ("% s \ n %-30s \ n %-30s \ n %-30s \ n %-30s \ n %-30s \ n %-30s \ n",
"Netstat written in Perl by FinalBSD. Copyright (c) 2008 .",
"-N Show numeric ip and port address .",
"-R Display the kernel routing table .",
"-A Show both listening and non-listening socktes .",
"-T Show only TCP statistics .",
"-L Show only listening sockets .",
"-H Show help .",
);
Exit;
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.