-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtcpdump-probe
executable file
·177 lines (148 loc) · 3.17 KB
/
tcpdump-probe
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#!/bin/bash
# This script runs tcpdump on a specified interface
# v1.2
#
# Version 1.0: Dump at specified interface
# Version 1.1: If no interface is specified dump on all interfaces
# Version 1.2: Dump all except 'lo'
#
# Copyright 2017 Wladimir Guerra
PATH=/bin:/usr/bin:/sbin:/usr/sbin
PCAP_FILES_PATH=/hostlab/_test/results/tcpdump
#TODO incluir criação de PCAP_FILE_PATH se não existir
USAGE_MESSAGE="
Usage: $(basename $0) {start|stop|status|restart|force-reload} [interface]
:. E.g. '$(basename $0) start eth0' whoud start to monitor interface eth0
"
# Print the name of the pidfile for the parsed interface
pidfile()
{
# the $1 artument is the interface to monitor
echo "/var/run/tcpdump-$1.pid"
}
# Get pid number at the pidfile for the parsed inerface
get_pid()
{
# the $1 argument is the interface to monitor
cat `pidfile $1` 2>/dev/null
}
start()
{
interface="$1"
pid_file=`pidfile $interface`
pid=`get_pid $interface`
#pcap_file_index=$(ls -l "$PCAP_FILES_PATH/"| egrep "dump-[0-9]*-$(hostname)-${interface}\\.pcap" | wc -l)
pcap_file_index=$(pcap-file-index "$PCAP_FILES_PATH")
[ -z "$pcap_file_index" ] && pcap_file_index=0
pcap_file=$PCAP_FILES_PATH/dump-${pcap_file_index}-$(hostname)-${interface}.pcap
if kill -0 $pid 2>/dev/null ; then
echo "tcpdump already running on $interface"
return 0
fi
echo "Starting tcpdump on ${interface}"
echo "Data is recording at $pcap_file"
start-stop-daemon \
--start \
--make-pidfile \
--pidfile $pid_file \
--background \
--exec /usr/sbin/tcpdump \
-- \
-s 0 \
-w $pcap_file \
-U \
-i $interface \
-v \
-tttt
}
stop()
{
pid_file=`pidfile $1`
echo "Stopping monitoring $1..."
start-stop-daemon \
--stop \
--quiet \
--oknodo \
--pidfile $pid_file
if [[ $? -eq 0 ]] ; then
rm $pid_file 2>/dev/null
fi
echo "Monitoring of $1 is stopped."
}
# --status option doesn't work on start-stop-daemon on netkit
status()
{
# pid_file= `pidfile $1`
pid=`get_pid $1`
interface=$1
if kill -0 $pid 2>/dev/null; then
echo "tcpdump is monitoring $interface"
return 0
else
echo "tcpdump is not monitoring $interface"
return 0
fi
# start-stop-daemon \
# --status \
# --pidfile $pid_file
# case "$?" in
# 0)
# echo "tcpdump is running on $interface\n"
# ;;
# 1)
# echo "tcpdump is not running and the pid file $pid_file exist. It can and shoud be deleted.\n"
# rm -i $pid_file
# ;;
# 3)
# echo "tcpdump is not running\n"
# ;;
# 4)
# echo "unable to determine program status\n"
# ;;
# esac
}
################
# MAIN PROGRAM #
################
# The number of arguments must be 2
#if [ $# -ne 2 ]; then
# echo "$USAGE_MESSAGE"
# exit 1
#fi
case "$1" in
start)
if [ -z $2 ]; then
echo "Starting to monitor all interfaces"
for interf in $(ls /sys/class/net); do
if [ "$interf" != "lo" ] && [ "$interf" != "teql0" ]; then
start "$interf"
fi
done
else
start $2
fi
;;
stop)
if [ -z $2 ]; then
echo "Stoping to monitor all interfaces"
for interf in $(ls /sys/class/net); do
stop "$interf"
done
else
stop $2
fi
;;
status)
status $2
;;
restart|force-reload)
$0 stop $2
sleep 1
$0 start $2
;;
*)
echo $USAGE_MESSAGE
exit 1
;;
esac
exit 0