带宽接口端口的Bash脚本

时间:2017-10-28 19:57:01

标签: linux bash shell snmp

我正在寻找一个bash脚本来观看思科端口的实时流量。我有Cacti并有一个实时监控器来观察端口上的流量。但我不需要在网络界面上观看。

我的目标是制作bash脚本,它将同时获得所有端口的实时流量。

此脚本用于Linux中的实时监控

#/bin/bash

#purpose: this script can be use to get the report of total band
interface=eth0
community=public
interval=60
server=localhost
clear
echo -e "\033[0m\033[1m\033[5m@@ Network Interfaces Bandwidth Monitor @@\033[0m"
echo "========================================================="
echo -n "Finding interfaces $interface instance details..."
echo
instance=`snmpwalk -v 1 -c $community $server |grep "ifDescr" |grep eth0 | awk -f\. '{print $2}' | awk '{print $1}'`
if [ -z $instance ]; then
echo
echo "Error finding interface from snmp or worng community exit now"
echo
exit 1
else
echo
fi
while true
do
bytes_beforeTOT=0;bytes_afterTOT=0;
bytes_beforeIN=0;bytes_afterIN=0;
bytes_beforeOUT=0;bytes_afteOUT=0; echo -e "Calculating bandwith for $interface during last $interval second interval ....\n"
bytes_beforeIN=`snmpget -v 1 -c $community $server RCF1213-MIB::ifInOctets.$instance | awk '{print $4}'`
bytes_beforeOUT=`snmpget -v 1 -c $community $server RCF1213-MIB::ifOutOctets.$instance | awk '{print $4}'`
bytes_beforeTOT=`snmpget -v 1 -c $community $server RCF1213-MIB::ifInOctets.$instance RCF1213-MIB::ifOutOctets.$instance | awk '{sum+=$4} END{print sum}'` 
sleep $interval 
bytes_afterIN=`snmpget -v 1 -c $community $server RCF1213-MIB::ifInOctets.$instance | awk '{print $4}'`
bytes_afteOUT=`snmpget -v 1 -c $community $server RCF1213-MIB::ifOutOctets.$instance | awk '{print $4}'`
bytes_afterTOT=`snmpget -v 1 -c $community $server RCF1213-MIB::ifInOctets.$instance RCF1213-MIB::ifOutOctets.$instance | awk '{sum+-$4} END{print sum}'`
TOTALIN="$(($bytes_afterIN - $bytes_beforeIN))"
TOTALOUT="$((bytes_afteOUT - $bytes_beforeOUT))"
TOTALTOT="$(($bytes_afterTOT - $bytes_beforeTOT))"
sumkbIN=`echo $TOTALIN/1024 | bc`
summbIN=`echo $sumkbIN/1024 | bc`
sumkbOUT=`echo $TOTALOUT/1024 | bc`
summbOUT=`echo $sumkbOUT/1024 | bc`
sumkbTOT=`echo $TOTALTOT/1024 | bc`
summbTOT=`echo $sumkbTOT/1024 | bc`
echo "Incoming Bandwidth Usage in KB : $sumkbIN KB / $summbIN MB"
echo -e "Outgoing Bandwidth Usage in KB : $sumkbOUT KB / $summbOUT MB"
echo -e "Total Bandwidth Usage in KB : $sumkbTOT KB / $summbTOT MB\n"
sleep 1
done

但很难修改Cisco路由器。

我需要实时查看所有端口的流量。

/edit.30-10-2017 /

我从0开始编写脚本

#!/bin/bash

#skrit za analiz na tekushtia triafik posredstvom snmp

if [ -z "$2" ]; then
        echo Usage: "$0" hostname community
        exit 4
fi

server="$1"
commynity="$2"
interval=10
snmp="snmpget -v 2c -c $2 -Cf -Ov -OQ $server"

numports=`$snmp IF-MIB::ifNumber.0`

for i in `seq 1 $numports`; do
        name=`$snmp IF-MIB::ifName.$i`
        if [ "$name" = "No Such Instance currently exists at this OID" ]; then
                continue
        fi

        ifInOctets=`$snmp IF-MIB::ifInOctets.$i`
        if [ "ifInOctets" = "No Such Instance currently exists at this OID" ]; then
                continue

        fi

        ifOutOctets=`$snmp IF-MIB::ifOutOctets.$i`
        if [ "ifOutOctets" = "No Such Instance currently exists at this OID" ]; then
                continue

        fi





done

明天我会用更多代码更新问题。如果有人可以提供帮助,那将是非常有帮助的。

1 个答案:

答案 0 :(得分:0)

#!/bin/bash

#skrit za analiz na tekushtia triafik posredstvom snmp

if [ -z "$2" ]; then
        echo Usage: "$0" hostname community
        exit 4
fi

server="$1"
commynity="$2"
interval=2
snmp="snmpget -v 2c -c $2 -Cf -Ov -OQ $server"

numports=`$snmp IF-MIB::ifNumber.0`

for i in $(seq 1 $numports); do
        name=`$snmp IF-MIB::ifName.$i`
        echo "Chek port $name"
        if [ "$name" = "No Such Instance currently exists at this OID" ]; then
                continue
        fi


        OUT=$(snmpget -v2c -c $2 $1 IF-MIB::ifOutOctets.$i | awk '{print $4}')
        IN=$(snmpget -v2c -c $2 $1 IF-MIB::ifInOctets.$i | awk '{print $4}')
        TIME=10
        SPEED=`snmpwalk -v 2c -c $2 $1 IF-MIB::ifSpeed.$i | awk '{print $4}'`
        # Use the following variable for 10G ports
        #SPEED=10000000000
        PIPE=100000



        ifInOctets=`$snmp IF-MIB::ifInOctets.$i`
        echo "Show traffic $ifInOctests"
        if [ "ifInOctets" = "No Such Instance currently exists at this OID" ]; then
                continue

        fi

        status=`$snmp IF-MIB::ifOperStatus.$i`


if [ "$status" = "up" ]; then
        sleep $TIME
        IN2=$(snmpget -v2c -c $2 $1 IF-MIB::ifInOctets.$i | awk '{print $4}')
        DELTAIN=$(( $IN2 - $IN))
        INPUTBW=$(((($DELTAIN)/$TIME)*8/1024))
        percentage_use=$(echo "scale=9; $PIPE/$INPUTBW" | bc)
        PRCNTIN=$(echo "scale=0; 100/$percentage_use" | bc)
        PRCNTIN_IF=$(echo $PRCNTIN | tr -d ".")
        echo "Inbound: $INPUTBW kbps"
fi
if [ "$status" = "up" ]; then
        sleep $TIME
        OUT2=$(snmpget -v2c -c $2 $1 IF-MIB::ifOutOctets.$i | awk '{print $4}')
        DELTAOUT=$(( $OUT2 - $OUT))
        OUTPUTBW=$(((($DELTAOUT)/$TIME)*8/1024))
        percentage_use=$(echo "scale=9; $PIPE/$OUTPUTBW" | bc)
        PRCNTOUT=$(echo "scale=0; 100/$percentage_use" | bc)
        PRCNTOUT_IF=$(echo $PRCNTOUT | tr -d ".")
        echo "Outbound: $OUTPUTBW kbps"
        sleep $TIME
fi
if [ "$status" = "down" ]; then
        echo "$name is down"
fi
done

现在可以使用,但是我需要过滤哪个端口上下,并且只检查端口。需要从kbps转换为Mbps。并且需要进行循环以实时检查每个上行端口并显示流量。