Path: blob/master/DDOS Scripts/AMP YUBINA SCRIPTS/pps.sh
4607 views
#!/bin/bash12usage()3{4cat <<EOF56Calculates PPS, BPS, and percentage of line-rate (LR).7from Linux kernel statistics by reading from procfs.89Options:1011-i interface(s) Interface or interfaces (sep:,) e.g. \`\`-i eth0,eth1,eth2''12-t <int> Time interval in seconds (def: 1)1314EOF15}1617argcheck() {18if [ $ARGC -lt $1 ]; then19echo "Please specify an argument!, try $0 -h for more information"20exit 121fi22}2324INTERVAL=125END='\e[m'26RED='\e[0;31m'27BLUE='\e[0;34m'28ORANGE='\e[0;33m'29ARGC=$#3031# Check for dependency filesystems32if ! [ -d /sys ] || ! [ -d /proc ]; then33echo "$0 requires sysfs and procfs"34exit $UNKNOWN35fi3637# Print warning and exit if less than n arguments specified38argcheck 13940# option and argument handling41while getopts "hi:t:" OPTION42do43case $OPTION in44h)45usage46exit47;;48i)49INTERFACES=$(echo $OPTARG | sed 's/,/ /g')50;;51t)52INTERVAL=$OPTARG53;;54*)55usage56exit57;;58esac59done6061echo -e "${BLUE}^C to exit${END}"6263while true; do64for INT in $INTERFACES; do65# Get speed of NIC66speed=$(cat /sys/class/net/$INT/speed)6768# Get number of packets for interface69rxppsold=$(awk "/$INT/ "'{ sub(":", " "); print $3 }' /proc/net/dev)70txppsold=$(awk "/$INT/ "'{ sub(":", " "); print $11 }' /proc/net/dev)7172# Get number of bytes for interface73rxbytesold=$(awk "/$INT/ "'{ sub(":", " "); print $2 }' /proc/net/dev)74txbytesold=$(awk "/$INT/ "'{ sub(":", " "); print $10 }' /proc/net/dev)7576sleep $INTERVAL7778# Get number of packets for interface again and subtract from old79rxppsnew=$(awk -v rxppsold="$rxppsold" "/$INT/ "'{ \80sub(":", " "); rxppsnew = $3; print rxppsnew - rxppsold }' /proc/net/dev)81txppsnew=$(awk -v txppsold="$txppsold" "/$INT/ "'{ \82sub(":", " "); txppsnew = $11; print txppsnew - txppsold }' /proc/net/dev)8384# Get number of bytes for interface again and subtract from old85rxbytesnew=$(awk -v rxbytesold="$rxbytesold" "/$INT/ "'{ \86sub(":", " "); rxbytesnew = $2; print rxbytesnew - rxbytesold }' /proc/net/dev)87txbytesnew=$(awk -v txbytesold="$txbytesold" "/$INT/ "'{ \88sub(":", " "); txbytesnew = $10; print txbytesnew - txbytesold }' /proc/net/dev)8990# Calculate percentage of line-rate from number of bytes per second.91rxlinerate=$(($rxbytesnew / 125000 / $speed * 100))92txlinerate=$(($txbytesnew / 125000 / $speed * 100))9394# Format line-rate values by truncating after the 1000th decimal place.95rxlr=$(printf "%1.3f" $rxlinerate)96txlr=$(printf "%1.3f" $txlinerate)9798# Print the results99echo -e "${ORANGE}Int${END}: $RED${INT}$END ${BLUE}|${END} $ORANGE[$END${RED}RX${END}$ORANGE]$END ${ORANGE}PPS${END}: $RED${rxppsnew}$END ${BLUE}|${END} ${ORANGE}BPS${END}: $RED${rxbytesnew}$END ${BLUE}|${END} ${ORANGE}% of LR${END}: ${RED}$rxlr${END} ${BLUE}--${END} $ORANGE[$END${RED}TX${END}$ORANGE]$END ${ORANGE}PPS${END}: $RED${txppsnew}$RED ${BLUE}|${END} ${ORANGE}BPS${END}: ${RED}$txbytesnew${END} ${BLUE}|${END} ${ORANGE}% of LR${END}: ${RED}$txlr${END}"100101done102done103104105