Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
R00tS3c
GitHub Repository: R00tS3c/DDOS-RootSec
Path: blob/master/DDOS Scripts/AMP YUBINA SCRIPTS/pps.sh
4607 views
1
#!/bin/bash
2
3
usage()
4
{
5
cat <<EOF
6
7
Calculates PPS, BPS, and percentage of line-rate (LR).
8
from Linux kernel statistics by reading from procfs.
9
10
Options:
11
12
-i interface(s) Interface or interfaces (sep:,) e.g. \`\`-i eth0,eth1,eth2''
13
-t <int> Time interval in seconds (def: 1)
14
15
EOF
16
}
17
18
argcheck() {
19
if [ $ARGC -lt $1 ]; then
20
echo "Please specify an argument!, try $0 -h for more information"
21
exit 1
22
fi
23
}
24
25
INTERVAL=1
26
END='\e[m'
27
RED='\e[0;31m'
28
BLUE='\e[0;34m'
29
ORANGE='\e[0;33m'
30
ARGC=$#
31
32
# Check for dependency filesystems
33
if ! [ -d /sys ] || ! [ -d /proc ]; then
34
echo "$0 requires sysfs and procfs"
35
exit $UNKNOWN
36
fi
37
38
# Print warning and exit if less than n arguments specified
39
argcheck 1
40
41
# option and argument handling
42
while getopts "hi:t:" OPTION
43
do
44
case $OPTION in
45
h)
46
usage
47
exit
48
;;
49
i)
50
INTERFACES=$(echo $OPTARG | sed 's/,/ /g')
51
;;
52
t)
53
INTERVAL=$OPTARG
54
;;
55
*)
56
usage
57
exit
58
;;
59
esac
60
done
61
62
echo -e "${BLUE}^C to exit${END}"
63
64
while true; do
65
for INT in $INTERFACES; do
66
# Get speed of NIC
67
speed=$(cat /sys/class/net/$INT/speed)
68
69
# Get number of packets for interface
70
rxppsold=$(awk "/$INT/ "'{ sub(":", " "); print $3 }' /proc/net/dev)
71
txppsold=$(awk "/$INT/ "'{ sub(":", " "); print $11 }' /proc/net/dev)
72
73
# Get number of bytes for interface
74
rxbytesold=$(awk "/$INT/ "'{ sub(":", " "); print $2 }' /proc/net/dev)
75
txbytesold=$(awk "/$INT/ "'{ sub(":", " "); print $10 }' /proc/net/dev)
76
77
sleep $INTERVAL
78
79
# Get number of packets for interface again and subtract from old
80
rxppsnew=$(awk -v rxppsold="$rxppsold" "/$INT/ "'{ \
81
sub(":", " "); rxppsnew = $3; print rxppsnew - rxppsold }' /proc/net/dev)
82
txppsnew=$(awk -v txppsold="$txppsold" "/$INT/ "'{ \
83
sub(":", " "); txppsnew = $11; print txppsnew - txppsold }' /proc/net/dev)
84
85
# Get number of bytes for interface again and subtract from old
86
rxbytesnew=$(awk -v rxbytesold="$rxbytesold" "/$INT/ "'{ \
87
sub(":", " "); rxbytesnew = $2; print rxbytesnew - rxbytesold }' /proc/net/dev)
88
txbytesnew=$(awk -v txbytesold="$txbytesold" "/$INT/ "'{ \
89
sub(":", " "); txbytesnew = $10; print txbytesnew - txbytesold }' /proc/net/dev)
90
91
# Calculate percentage of line-rate from number of bytes per second.
92
rxlinerate=$(($rxbytesnew / 125000 / $speed * 100))
93
txlinerate=$(($txbytesnew / 125000 / $speed * 100))
94
95
# Format line-rate values by truncating after the 1000th decimal place.
96
rxlr=$(printf "%1.3f" $rxlinerate)
97
txlr=$(printf "%1.3f" $txlinerate)
98
99
# Print the results
100
echo -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}"
101
102
done
103
done
104
105