Generalize ttcn3-tcpdump-*.sh

Prepare for supporting alternative packet dumpers:
* reword comments
* rename pidfile
* move tcpdump-specific option inside if
* move comment about sudo closer to actual sudo invocation

Those are cosmetic changes which do not affect how packet dump is made
but makes it easier to support alternative packet dumpers in follow-up
commit.

Change-Id: Ib2528db65348c0422fe8b7c7c53656fbce4f6405
This commit is contained in:
Max 2019-03-14 18:15:27 +01:00
parent 6e05304843
commit e521445fee
2 changed files with 14 additions and 14 deletions

View File

@ -1,6 +1,6 @@
#!/bin/sh #!/bin/sh
PIDFILE=/tmp/tcpdump.pid PIDFILE=/tmp/dumper.pid
TCPDUMP=/usr/sbin/tcpdump TCPDUMP=/usr/sbin/tcpdump
TESTCASE=$1 TESTCASE=$1
@ -16,19 +16,19 @@ if [ -e $PIDFILE ]; then
rm $PIDFILE rm $PIDFILE
fi fi
if [ "$(id -u)" = "0" ]; then
CMD="$TCPDUMP -U"
else
# NOTE: This requires you to be root or something like # NOTE: This requires you to be root or something like
# "laforge ALL=NOPASSWD: /usr/sbin/tcpdump, /bin/kill" in your sudoers file # "laforge ALL=NOPASSWD: /usr/sbin/tcpdump, /bin/kill" in your sudoers file
if [ "$(id -u)" = "0" ]; then CMD="sudo $TCPDUMP -U"
CMD=$TCPDUMP
else
CMD="sudo $TCPDUMP"
fi fi
$CMD -U -s 1500 -n -i any -w "$TTCN3_PCAP_PATH/$TESTCASE.pcap" >$TTCN3_PCAP_PATH/$TESTCASE.pcap.stdout 2>&1 & $CMD -s 1500 -n -i any -w "$TTCN3_PCAP_PATH/$TESTCASE.pcap" >$TTCN3_PCAP_PATH/$TESTCASE.pcap.stdout 2>&1 &
PID=$! PID=$!
echo $PID > $PIDFILE echo $PID > $PIDFILE
# Wait until tcpdump creates the pcap file and starts recording. # Wait until packet dumper creates the pcap file and starts recording.
# We generate some traffic until we see tcpdump catches it. # We generate some traffic until we see packet dumper catches it.
# Timeout is 10 seconds. # Timeout is 10 seconds.
ping 127.0.0.1 >/dev/null 2>&1 & ping 127.0.0.1 >/dev/null 2>&1 &
PID=$! PID=$!
@ -36,7 +36,7 @@ i=0
while [ ! -f "$TTCN3_PCAP_PATH/$TESTCASE.pcap" ] || while [ ! -f "$TTCN3_PCAP_PATH/$TESTCASE.pcap" ] ||
[ "$(stat -c '%s' "$TTCN3_PCAP_PATH/$TESTCASE.pcap")" -eq 32 ] [ "$(stat -c '%s' "$TTCN3_PCAP_PATH/$TESTCASE.pcap")" -eq 32 ]
do do
echo "Waiting for tcpdump to start... $i" echo "Waiting for packet dumper to start... $i"
sleep 1 sleep 1
i=$((i+1)) i=$((i+1))
if [ $i -eq 10 ]; then if [ $i -eq 10 ]; then

View File

@ -1,6 +1,6 @@
#!/bin/sh #!/bin/sh
PIDFILE=/tmp/tcpdump.pid PIDFILE=/tmp/dumper.pid
TESTCASE=$1 TESTCASE=$1
VERDICT="$2" VERDICT="$2"
@ -17,14 +17,14 @@ if [ "z$TTCN3_PCAP_PATH" = "z" ]; then
TTCN3_PCAP_PATH=/tmp TTCN3_PCAP_PATH=/tmp
fi fi
# Wait for up to 2 seconds if we keep receiving traffinc from tcpdump, # Wait for up to 2 seconds if we keep receiving traffinc from packet dumper,
# otherwise we might lose last packets from test. # otherwise we might lose last packets from test.
i=0 i=0
prev_count=-1 prev_count=-1
count=$(stat --format="%s" "$TTCN3_PCAP_PATH/$TESTCASE.pcap") count=$(stat --format="%s" "$TTCN3_PCAP_PATH/$TESTCASE.pcap")
while [ $count -gt $prev_count ] && [ $i -lt 2 ] while [ $count -gt $prev_count ] && [ $i -lt 2 ]
do do
echo "Waiting for tcpdump to finish... $i (prev_count=$prev_count, count=$count)" echo "Waiting for packet dumper to finish... $i (prev_count=$prev_count, count=$count)"
sleep 1 sleep 1
prev_count=$count prev_count=$count
count=$(stat --format="%s" "$TTCN3_PCAP_PATH/$TESTCASE.pcap") count=$(stat --format="%s" "$TTCN3_PCAP_PATH/$TESTCASE.pcap")
@ -32,11 +32,11 @@ do
done done
if [ -e $PIDFILE ]; then if [ -e $PIDFILE ]; then
# NOTE: This requires you to be root or something like
# "laforge ALL=NOPASSWD: /usr/sbin/tcpdump, /bin/kill" in your sudoers file
if [ "$(id -u)" = "0" ]; then if [ "$(id -u)" = "0" ]; then
kill "$(cat "$PIDFILE")" kill "$(cat "$PIDFILE")"
else else
# NOTE: This requires you to be root or something like
# "laforge ALL=NOPASSWD: /usr/sbin/tcpdump, /bin/kill" in your sudoers file
sudo kill "$(cat "$PIDFILE")" sudo kill "$(cat "$PIDFILE")"
fi fi
rm $PIDFILE rm $PIDFILE