Linux实时输出指定网卡流量

理论上能正常运行ifconfig的Linux操作系统都可适用这个脚本(当然一般默认会安装perl),打印实时网卡流量和丢包数,好久前当时为了验证输出流量随手写的,多次想改得漂亮点,一直没动手,就这样吧,以免用的时候找不到

其实我还想评论自己一句:把Perl写得跟C语言新手一样,真是Perl的悲哀!!!

#!/usr/bin/perl -w
####################################
#print bps, pkt dropped per second
#Can get four nic
#HuiLi
####################################

use 5.010;

print "Sorry, please run the script with a NIC name as ARGV !\n" and exit if !@ARGV;
my $G = 1024 * 1024 * 1024;
my $M = 1024 * 1024;
my $K = 1024;


sub traffic_data {

        my ($nic, $bps, $pkt) = @_;
        if ($bps >= $G) {
                $bps /= $G;
                print "Nic $nic         bps:    " .     substr($bps, 0, 5) .    "       Gbits/s                 dropped: $pkt pkts/s\n";
        } elsif ($bps >= $M and $bps < $G) {
                $bps /= $M;
                print "Nic $nic         bps:    " .     substr($bps, 0, 5) .    "       Mbits/s                 dropped: $pkt pkts/s\n";
        } elsif ($bps >= $K and $bps < $M) {
                $bps /= $K;
                print "Nic $nic         bps:    " .     substr($bps, 0, 5) .    "       Kbits/s                 dropped: $pkt pkts/s\n";
        } elsif ($bps < $K and $bps >= 0) {
                print "Nic $nic         bps:    " .     substr($bps, 0, 5) .    "       bits/s                  dropped: $pkt pkts/s\n";
        } else {
                print "Nic $nic     bps:    " . 0          .    "   bits/s          dropped:    $pkt pkts/s\n";
        }
}

sub nic_traffic {
        my ($nic, $flag) = @_;
        my $drop = $traffic = $pkt = $bps = 0;
        state $last_traffic1 = $last_drop1 = 0;
        state $last_traffic2 = $last_drop2 = 0;
        state $last_traffic3 = $last_drop3 = 0;
        state $last_traffic4 = $last_drop4 = 0;
        open my($NIC), "ifconfig $nic |";
        while(<$NIC>){
                $drop = $1 if /\s+RX\s+packets.*dropped:(\d+)\s+.*/;
                $traffic = $1 if /\s+RX\s+bytes:(\d+)/;
        }
        close($NIC);
        if ($flag == 0){
                $pkt = $drop - $last_drop1;
                $bps = ($traffic - $last_traffic1) * 8;
                traffic_data($nic, $bps, $pkt);
                $last_traffic1 = $traffic;
                $last_drop1 = $drop;
        } elsif ($flag == 1){
                $pkt = $drop - $last_drop2;
                $bps = ($traffic - $last_traffic2) * 8;
                traffic_data($nic, $bps, $pkt);
                $last_traffic2 = $traffic;
                $last_drop2 = $drop;
        } elsif ($flag == 2){
                $pkt = $drop - $last_drop3;
                $bps = ($traffic - $last_traffic3) * 8;
                traffic_data($nic, $bps, $pkt);
                $last_traffic3 = $traffic;
                $last_drop3 = $drop;
        } elsif ($flag == 3){
                $pkt = $drop - $last_drop4;
                $bps = ($traffic - $last_traffic4) * 8;
                traffic_data($nic, $bps, $pkt);
                $last_traffic4 = $traffic;
                $last_drop4 = $drop;
        }
}

while(1){
        my $flag = 0;
        print "########################################################################\n";
        foreach my $nic (@ARGV) {
                nic_traffic($nic, $flag);
                $flag = ($flag + 1) % 4;
        }
        select(undef, undef, undef, 1);
}

运行的时候最多可以带4个参数,也就是最多可以查看4个网口流量情况,想加参数的自行修改nic_traffic函数里的if/else,打印的第一次请无视,会减去上一秒(而上一秒根本没有)的结果,后面都是后一秒减去前一秒的数据,运行如下:

[lihui@localhost ~]# ./nic_traffic.pl eth0 xge0 xge1
#######################################################################################
Nic eth0                bps:    13.33   Kbits/s                 dropped:        0 pkts/s
Nic xge0                bps:    9.073   Gbits/s                 dropped:        0 pkts/s
Nic xge1                bps:    0       bits/s                  dropped:        0 pkts/s
#######################################################################################
Nic eth0                bps:    23.27   Kbits/s                 dropped:        0 pkts/s
Nic xge0                bps:    9.303   Gbits/s                 dropped:        0 pkts/s
Nic xge1                bps:    0       bits/s                  dropped:        0 pkts/s
#######################################################################################
Nic eth0                bps:    11.57   Kbits/s                 dropped:        0 pkts/s
Nic xge0                bps:    9.494   Gbits/s                 dropped:        0 pkts/s
Nic xge1                bps:    0       bits/s                  dropped:        0 pkts/s
#######################################################################################
Nic eth0                bps:    9.187   Kbits/s                 dropped:        0 pkts/s
Nic xge0                bps:    9.360   Gbits/s                 dropped:        0 pkts/s
Nic xge1                bps:    0       bits/s                  dropped:        0 pkts/s
#######################################################################################
Nic eth0                bps:    10.39   Kbits/s                 dropped:        0 pkts/s
Nic xge0                bps:    9.490   Gbits/s                 dropped:        0 pkts/s
Nic xge1                bps:    0       bits/s                  dropped:        0 pkts/s
#######################################################################################
Nic eth0                bps:    13.14   Kbits/s                 dropped:        0 pkts/s
Nic xge0                bps:    9.370   Gbits/s                 dropped:        0 pkts/s
Nic xge1                bps:    0       bits/s                  dropped:        0 pkts/s

发表回复