2017-10-28 11 views
0

시스코 포트에서 실시간 트래픽을 볼 수있는 bash 스크립트를 찾고 있습니다. 나는 선인장이 있고 어떤 트래픽이 항구를 통과 하는지를 실시간으로 모니터하고있다. 하지만 웹 인터페이스에서 시청할 필요가 없습니다.대역폭 인터페이스 포트 용 Bash 스크립트

내 목표는 동시에 모든 포트에 대해 실시간 트래픽을 얻을 수있는 bash 스크립트를 작성하는 것입니다.

이 스크립트는

#/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/

내가 스크립트를

#!/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 

내일 내가 더 많은 코드와 질문을 업데이트합니다를 작성하는 0부터 시작합니다. 어떤 사람이 도움을 줄 수 있다면 큰 도움이 될 것입니다.

+0

스크립트에서'snmpget'을 한 번만 호출하십시오.'snmpget -v 1 -c $ community $ server RCF1213-MIB'를 사용하면 모든 세부 사항을 되돌리고 awk 스크립트로 구문 분석 할 수 있습니다. 현재 코드는 1 세트의 출력을 생성하기 위해 많은, 많은 프로세스를 실행하고 있습니다. 아마도'snmpwalk' 1 회와'smnpget' 1 회를 호출 한 다음 하나의 스크립트에서 모든 검색된 데이터를 처리해야합니다. 'smnpget' (etc) 요청에 응답하는 하드웨어에 대한 액세스 권한이 없으므로이 Q에 관해 당신을 도울 수 없게됩니다. 또는 최소한의 데이터로 Q를 업데이트하는 것을 고려해보십시오. 행운을 빕니다. – shellter

답변

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로 변환해야합니다. 그리고 루프를 만들어 포트마다 실시간으로 확인하고 트래픽을 표시해야합니다.