Created
April 14, 2023 19:53
-
-
Save movedx/5561b13fd08582891a7bdf233e7712bc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Set the port number you want to monitor | |
PORT=80 | |
# Set the output file to store the traffic data | |
OUTPUT_FILE="traffic_data.txt" | |
# Set the recipient email address | |
EMAIL_RECIPIENT="your.email@example.com" | |
# Set the subject of the email | |
EMAIL_SUBJECT="Weekly Traffic Data Report" | |
# Create the iptables rule to count the traffic | |
iptables -I INPUT -p tcp --dport $PORT | |
iptables -I OUTPUT -p tcp --sport $PORT | |
# Function to get the traffic data | |
get_traffic_data() { | |
iptables -L -n -v -x | grep -E "tcp dpt:${PORT}|tcp spt:${PORT}" | awk '{sum += $2} END {print sum}' | |
} | |
# Monitor the traffic and save the data to the file | |
for i in {1..60480}; do | |
traffic_data=$(get_traffic_data) | |
echo "$(date +"%Y-%m-%d %H:%M:%S") - Traffic data for port $PORT: $traffic_data bytes" >> $OUTPUT_FILE | |
sleep 10 | |
done | |
# Send the traffic data file via email | |
send_email() { | |
cat $OUTPUT_FILE | mail -s "$EMAIL_SUBJECT" $EMAIL_RECIPIENT | |
} | |
# Send the email with the traffic data | |
send_email | |
# Remove the file after sending the email | |
rm $OUTPUT_FILE |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment