2022-11-21 18:39:12 +00:00
|
|
|
#!/bin/bash
|
|
|
|
|
2023-05-15 06:26:55 +00:00
|
|
|
# enter in the pyPLC directory, so that all subsequent pathes can be relative to this.
|
|
|
|
cd "$( dirname "${BASH_SOURCE[0]}" )"
|
|
|
|
|
2023-05-13 17:35:54 +00:00
|
|
|
# Function to start tcpdump
|
|
|
|
start_tcpdump() {
|
|
|
|
INTERFACE="eth0" # Change this to the interface you want to monitor
|
|
|
|
OUTPUT_FILE="$1"
|
|
|
|
tcpdump -i "$INTERFACE" -w "$OUTPUT_FILE" &> /dev/null &
|
|
|
|
TCPDUMP_PID=$!
|
|
|
|
echo "Started tcpdump with PID $TCPDUMP_PID on interface $INTERFACE, output file: $OUTPUT_FILE"
|
|
|
|
}
|
|
|
|
# Function to stop tcpdump
|
|
|
|
stop_tcpdump() {
|
|
|
|
if [ ! -z "$TCPDUMP_PID" ]; then
|
|
|
|
echo "Stopping tcpdump with PID $TCPDUMP_PID"
|
|
|
|
kill -SIGINT "$TCPDUMP_PID"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
# Function to handle signals
|
|
|
|
signal_handler() {
|
|
|
|
echo "Caught signal, stopping tcpdump and exiting..."
|
|
|
|
stop_tcpdump
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
# Set trap for signals: SIGINT (2), SIGTERM (15)
|
|
|
|
trap 'signal_handler' 2 15
|
|
|
|
|
2023-05-15 06:26:55 +00:00
|
|
|
# Set options for the bash: exit on error, treat unused variables as error, print the input as it is read.
|
2023-05-13 17:35:54 +00:00
|
|
|
set -euv
|
|
|
|
|
|
|
|
# Keep all IPv6 addresses on the interface down event.
|
|
|
|
# Todo: flexible interface name.
|
2023-05-13 18:27:08 +00:00
|
|
|
# Todo: On raspberry, without NetworkManager, this option does not help. After the down and up (see below), the IPv6 is missing.
|
2023-05-13 17:35:54 +00:00
|
|
|
sysctl net.ipv6.conf.eth0.keep_addr_on_down=1
|
|
|
|
|
|
|
|
# Shut down and activate the interface.
|
2023-05-13 18:27:08 +00:00
|
|
|
# Todo: Why this needed? On raspberry, where the NetworkManager is not runnning, this disturbs, because
|
|
|
|
# afterwards the pyPlc does not see the interfaces IPv6 address.
|
2023-05-15 06:26:55 +00:00
|
|
|
# Todo: make this configurable, for the cases we need this.
|
2023-12-12 11:23:49 +00:00
|
|
|
ip link set eth0 down
|
|
|
|
ip link set can0 down
|
2023-05-13 17:35:54 +00:00
|
|
|
sleep 1
|
2023-12-12 11:23:49 +00:00
|
|
|
ip link set eth0 up
|
|
|
|
ip link set can0 up type can restart-ms 100 bitrate 500000
|
2023-05-13 17:35:54 +00:00
|
|
|
sleep 1
|
|
|
|
|
|
|
|
# show the addresses
|
|
|
|
ip addr
|
|
|
|
|
2023-05-15 06:26:55 +00:00
|
|
|
# print the current directory to log. Should be the pyPLC directory.
|
|
|
|
pwd
|
|
|
|
|
|
|
|
# create directory for the log files.
|
|
|
|
mkdir -p log
|
2023-05-13 17:35:54 +00:00
|
|
|
# prepare the file names for the log files
|
2023-12-12 11:23:49 +00:00
|
|
|
pushd log
|
|
|
|
gzip *.log || true
|
|
|
|
popd
|
2023-05-13 17:35:54 +00:00
|
|
|
date=$(date "+%Y-%m-%d_%H%M%S")
|
2023-12-12 11:23:49 +00:00
|
|
|
#logfile=./log/"$date"_pevNoGui.log
|
|
|
|
#logfile=./log/pevNoGui.log
|
|
|
|
index=`cat log/logindex`
|
|
|
|
logfile=`printf "log/%04d_pevNoGui.log" $index`
|
|
|
|
tcpdump_logfile=`printf "log/%04d_tcpdump.pcap" $index`
|
|
|
|
index=$(($index + 1))
|
|
|
|
echo $index > log/logindex
|
2023-05-13 17:35:54 +00:00
|
|
|
|
|
|
|
echo "logfile: $logfile"
|
|
|
|
echo "tcpdump_logfile: $tcpdump_logfile"
|
|
|
|
|
|
|
|
# start the tcpdump
|
2023-12-12 11:23:49 +00:00
|
|
|
#start_tcpdump "$tcpdump_logfile"
|
2023-05-13 17:35:54 +00:00
|
|
|
|
2023-12-12 11:23:49 +00:00
|
|
|
echo "$date" > "$logfile"
|
2023-05-13 17:35:54 +00:00
|
|
|
git log --oneline -1 >> "$logfile" || echo "Not a git repo" >> "$logfile"
|
|
|
|
ip addr >> "$logfile"
|
|
|
|
pwd >> "$logfile"
|
2023-05-15 06:26:55 +00:00
|
|
|
|
|
|
|
# call the pyPlc python script
|
2024-06-06 07:10:06 +00:00
|
|
|
#PYTHONUNBUFFERED=1 /usr/bin/python3 pevNoGui.py | tee -a "$logfile"
|
|
|
|
PYTHONUNBUFFERED=1 /usr/bin/python3 listenerNoGui.py | tee -a "$logfile"
|
2023-05-13 17:35:54 +00:00
|
|
|
pwd >> "$logfile"
|
|
|
|
date >> "$logfile"
|
|
|
|
|
|
|
|
# Stop the tcpdump when the pyPLC stopped:
|
|
|
|
stop_tcpdump
|
|
|
|
|