You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
mininet-project/routing-table-monitor.sh

69 lines
1.3 KiB
Bash

5 months ago
name="$(ifconfig | awk -F- '$2 ~ /eth/ { print $1; exit;}')"
5 months ago
log_directory=$HOME/logs/
log_file=${name}-rt.log
while [[ $# -gt 0 ]]; do
case $1 in
-c|--cmd)
RT_COMMAND="$2"
shift # past argument
shift # past value
;;
-o|--output)
OUTPUT_FILE="$2"
shift # past argument
shift # past value
;;
-*|--*)
echo "Unknown option $1"
exit 1
;;
esac
done
: "${RT_COMMAND:='route -n'}"
: "${OUTPUT_FILE:=${log_directory}${log_file}}"
echo "COMMAND: $RT_COMMAND"
echo "OUTPUT: $OUTPUT_FILE"
echo > "$OUTPUT_FILE"
echo "starting at: $(date +"%T %N")" >> "$OUTPUT_FILE"
5 months ago
function parse_diff () {
str=$(echo "$1" | sed '1,5d')
add_line=$(echo "$str" | grep -E '^\+')
rem_line=$(echo "$str" | grep -E '^-')
[ -n "$rem_line" ] && log "$(echo "$rem_line" | cut -c 2-)" "del:"
[ -n "$add_line" ] && log "$(echo "$add_line" | cut -c 2-)" "add:"
}
function log () {
while IFS= read -r line; do
5 months ago
echo "here"
echo -e "$(date +"%T %N")\t$2\t$line" >> "$OUTPUT_FILE"
5 months ago
done <<< "$1"
}
5 months ago
curr=$($RT_COMMAND)
5 months ago
atstart="$(echo "$curr" | sed '1,2d')"
log "$atstart" "add:"
while true;
do
prev="$curr"
5 months ago
curr=$($RT_COMMAND)
5 months ago
diff_out=$(diff -u <(echo "$prev") <(echo "$curr"))
[ $? -eq 0 ] || parse_diff "$diff_out"
5 months ago
sleep 0.5
5 months ago
done