0
我正在尋找一個bash腳本來觀看思科端口的實時流量。我有仙人掌,並有一個實時監控器來觀察通過端口的流量。但我不需要在Web界面中觀看。帶寬接口端口的Bash腳本
我的目標是製作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[[email protected]@ 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
明天我將與更多的代碼更新的問題。如果有人能提供幫助,這將會非常有幫助。
你應該嘗試在腳本中只調用'snmpget'一次,也許'snmpget -v 1 -c $ community $ server RCF1213-MIB'來取回所有細節,然後用awk腳本解析它。你當前的代碼運行很多很多的過程來產生一組輸出,你可能只需要調用'snmpwalk'' 1次和'smnpget''一次,然後在一個腳本中處理所有檢索到的數據。我無法訪問任何響應'smnpget'(etc)請求的硬件,因此我無法幫助您處理此問題(或者考慮使用最少量的數據更新您的Q)。祝你好運。 – shellter