-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpvcapture.sh
More file actions
executable file
·72 lines (59 loc) · 1.07 KB
/
pvcapture.sh
File metadata and controls
executable file
·72 lines (59 loc) · 1.07 KB
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
#!/usr/bin/env bash
set -euo pipefail
#set -x
usage() {
cat <<'USAGE'
Usage: pvcapture.sh <port> [port...]
Capture TCP traffic for one or more ports using dumpcap.
Options:
-h, --help Show this help
USAGE
}
# Parse arguments with getopt
PARSED_ARGS=$(getopt -o h --long help -- "$@")
if [ $? -ne 0 ]; then
usage
exit 2
fi
# Safe eval of getopt output
# shellcheck disable=SC2086
eval set -- "$PARSED_ARGS"
while true; do
case "$1" in
-h|--help)
usage
exit 0
;;
--)
shift
break
;;
*)
usage
exit 2
;;
esac
done
if [ $# -lt 1 ]; then
usage
exit 2
fi
FILTER=""
for port in "$@"; do
if [ -z "$FILTER" ]; then
FILTER="tcp port $port"
else
FILTER="$FILTER or tcp port $port"
fi
done
DUMPCAP_PID=""
cleanup() {
if [ -n "$DUMPCAP_PID" ] && kill -0 "$DUMPCAP_PID" 2>/dev/null; then
kill -INT "$DUMPCAP_PID" 2>/dev/null || true
wait "$DUMPCAP_PID" 2>/dev/null || true
fi
}
trap cleanup INT TERM EXIT
dumpcap -i lo -f "$FILTER" -w - &
DUMPCAP_PID=$!
wait "$DUMPCAP_PID"