title: Bash script for bandwidth interface ports
body: <p>I am looking for a bash script to watch live traffic in Cisco ports. I have Cacti and have a real time monitor to watch what traffic passes on ports. But I don`t need to watch in web interface.</p>
  
  <p>My aim is to make bash script which will get real time traffic for all ports at the same time.</p>
  
  <p>This script is used for live monitoring in Linux</p>
  
  <pre><code>#/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
  </code></pre>
  
  <p>but it is hard to modify for Cisco routers.</p>
  
  <p>I need to watch traffic in all ports real time.</p>
  
  <p>/edit.30-10-2017/</p>
  
  <p>I start from 0 to write script</p>
  
  <pre><code>#!/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
  </code></pre>
  
  <p>Tomorrow I will update the question with more code. It would be of great help if some one can offer help.</p>