bash模拟netstat取值的脚本

 

如题

#! /bin/bash

function netstat()
{
        file="$1"
        Key="$2"
        res=`cat $file`
        title=`echo "$res" |grep $Key |head -n 1`
        value=`echo "$res" |grep $Key |tail -n 1`
        num=`echo $title |awk '{print NF}'`

        for ((i=2; i<$num+1; i++))
        do
                t=`echo "$title" |cut -d' ' -f "$i"`
                v=`echo "$value" |cut -d' ' -f "$i"`
                echo "tong_netstat_title:$t; tong_netstat_value=$v"
        done
}

netstat "/proc/net/netstat" "TcpExt:"
netstat "/proc/net/netstat" "IpExt:"
netstat "/proc/net/netstat" "IpExt:"
netstat "/proc/net/snmp" "Ip:"
netstat "/proc/net/snmp" "Icmp:"
netstat "/proc/net/snmp" "IcmpMsg:"
netstat "/proc/net/snmp" "Tcp:"
netstat "/proc/net/snmp" "Udp:"
netstat "/proc/net/snmp" "UdpLite:"

 

posted on 2025-07-21 14:56  toong  阅读(5)  评论(0)    收藏  举报