This repository has been archived by the owner on Feb 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
checkusage.sh
110 lines (101 loc) · 2.59 KB
/
checkusage.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/bin/sh
#
# CheckUsage
#
# Author: iernie
# Version: 1.2.1
# Date: 20111002
# Github: https://github.com/iernie/CheckUsage
# Dependencies: vnstat installed and running
######################
### CONFIGURATIONS ###
######################
LIMIT=8 # Limit for network traffic
LIMITSTR=GiB # The unit to check after; GiB, MiB, KiB
PREFIX="XX:XX:XX:XX:XX" # The prefix for the mac you want to use. Note: Use only the first five numbers.
INTERFACE="eth1" # The interface for your wan
############
### CODE ###
############
DATE=`date +%D`
OUTPUT=`vnstat -d | grep $DATE | cut -d\| -f3`
AMOUNT=$(echo $OUTPUT | cut -d" " -f1 | cut -d"." -f1)
UNIT=$(echo $OUTPUT | cut -d" " -f2)
IF=`ifconfig $INTERFACE | grep HWaddr | cut -d":" -f7`
CURRENT=$(echo $IF | cut -d" " -f1)
changeMac() {
echo "Old MAC: $PREFIX:$CURRENT"
if [ $CURRENT -lt 50 ]; then
NEW=`expr $CURRENT + 1`
MAC="$PREFIX:$NEW"
else
MAC="$PREFIX:10"
fi
echo "New MAC: $MAC"
ifdown
ifconfig $INTERFACE down
uci set network.wan.macaddr="$MAC"
uci commit network
ifconfig $INTERFACE hw ether $MAC
ifconfig $INTERFACE up
ifup wan
echo "Mac Change Complete!"
}
getDivisionNumber() {
if [ $UNIT == $LIMITSTR ]; then
DIV=$(($AMOUNT / $LIMIT))
FLOOR=$(echo $DIV | cut -d"." -f1)
echo "$FLOOR"
else
echo "0"
fi
}
getFormattedDate() {
MONTH=`date +%m`
YEAR=`date +%Y`
DAY=`date +%d`
echo "$YEAR$MONTH$DAY"
}
getLastChanged() {
NUMB=`getDivisionNumber`
DATE=`getFormattedDate`
echo "$NUMB:$DATE"
}
updateLastChanged() {
rm lastchange
getLastChanged >> lastchange
}
case "$1" in
force)
echo "Force changing MAC..."
changeMac
updateLastChanged
;;
*)
if ! [[ -f lastchange ]]; then
echo "0:19700101" >> lastchange
fi
LAST=`cat lastchange`
LASTNUMB=$(echo $LAST | cut -d":" -f1 | cut -d"." -f1)
NUMB=`getDivisionNumber`
LASTDATE=$(echo $LAST | cut -d":" -f2)
DATE=`getFormattedDate`
if [ $UNIT == $LIMITSTR ]; then
if [ $DATE -gt $LASTDATE ] && [ $AMOUNT -ge $LIMIT ]; then
echo "Total network traffic has exceeded limit: $AMOUNT $UNIT / $LIMIT $LIMITSTR"
changeMac
updateLastChanged
ifconfig $INTERFACE
elif [ $DATE == $LASTDATE ] && [ $NUMB -gt $LASTNUMB ]; then
echo "Total network traffic has once again exceeded limit: $AMOUNT $UNIT / $LIMIT $LIMITSTR"
changeMac
updateLastChanged
ifconfig $INTERFACE
else
echo "Either limit not exceeded or already changed mac today."
fi
else
echo "Unit is not right."
fi
esac
exit 0