Frank Helk
asked on
How to get tcpdump to capture continuous ?
I have some SLES 12.2 server where I need to monitor certain network traffic for diagnosing a problem that occurs every now an then. I'll try to record the traffic with tcpdump, and when that problem arises, I could dissect the correcponding network traffic with Wireshark.
I've set up a main script which contains
I'd expect that script to run indefinitely, creating capture files containing 30 minutes of data each, until I stop tcpdump with i.e. [CTRL-C] or kill. The postprocessing called after stopping (and whenever a new capture file is created) will zip the created capture files and limit the backlog of capture files to 12 hours.
So far, so good. Now to the problem:
tcpdump stops capturing data in the middle of the second file and exits (without error, as far as I could see).
What have I missed ?
I've set up a main script which contains
#!/bin/bash
tcpdump -iany -G $((30*60)) -n -w tcpdump.io.%F_%H%M%S.pcap -z ./tcpdump_postproc.sh net 192.168.1.0/24 or net 192.168.2.0/24 > tcpdump.statistics
./tcpdump_postproc.sh
and a helper script tcpdump_postproc.sh for some postprocessing:#!/bin/bash
gzip *.pcap
find . -maxdepth 0 -mmin +$((12*60)) -name '*.pcap.gz' -delete
I'd expect that script to run indefinitely, creating capture files containing 30 minutes of data each, until I stop tcpdump with i.e. [CTRL-C] or kill. The postprocessing called after stopping (and whenever a new capture file is created) will zip the created capture files and limit the backlog of capture files to 12 hours.
So far, so good. Now to the problem:
tcpdump stops capturing data in the middle of the second file and exits (without error, as far as I could see).
What have I missed ?
is it possible the disk was full when tcpdump stopped?
using echo $? you can get an exit status can you tell which one? It might be the only message given.
Did tcpdump_postproc.sh terminate with errors? (not exit 0)
Instead of tcpdump, you may want to look at tshark, the command line only companion to wireshark.
it can more or less do the same. IMHO it has better disectors. Also do not run tcpdump/t-shark/wireshark as root while dissecting protocols.
The disectors and other modules have not been scrutinized for buffer overflows etc.
using echo $? you can get an exit status can you tell which one? It might be the only message given.
Did tcpdump_postproc.sh terminate with errors? (not exit 0)
Instead of tcpdump, you may want to look at tshark, the command line only companion to wireshark.
it can more or less do the same. IMHO it has better disectors. Also do not run tcpdump/t-shark/wireshark as root while dissecting protocols.
The disectors and other modules have not been scrutinized for buffer overflows etc.
ASKER
Ok ... I've investigated a bit further. First, the version info of tcpdump is
I've changed the call to tcpdump to cycle every 10 minutes and ran it again. Looks like I didn't looked precisely on its behaviour.
The first generated capture file is built correctly with packes in the expected time frame.
But any file created afterwards seems to be completely empty (size = 0 bytes, with 54 byte zipped size ...).
Any hint ?
P.S.: No trace of problems in /var/log/messages or journalctl (both show no signs of tcpdump)
tcpdump version 4.9.0
libpcap version 1.8.1
OpenSSL 1.0.2j-fips 26 Sep 2016
SMI-library: 0.4.8
I've changed the call to tcpdump to cycle every 10 minutes and ran it again. Looks like I didn't looked precisely on its behaviour.
The first generated capture file is built correctly with packes in the expected time frame.
But any file created afterwards seems to be completely empty (size = 0 bytes, with 54 byte zipped size ...).
Any hint ?
P.S.: No trace of problems in /var/log/messages or journalctl (both show no signs of tcpdump)
As noci suggested, once tcpdump starts it continues + never... just stops... unless some catastrophic error occurs, like full disk.
Add echo $? as noci suggested + report what error occurs.
Add echo $? as noci suggested + report what error occurs.
ASKER
Just to clarify, as I've seen now, tcpdump DOESN'T ABORT. I have to stop it with CRTL-C or kill.
The first file is created correct, all subsequent capture files come up empty. Looks like cycling the output file breaks something w/o stopping tcpdump. After half an hour the doirectory looks like this:
The first file is created correct, all subsequent capture files come up empty. Looks like cycling the output file breaks something w/o stopping tcpdump. After half an hour the doirectory looks like this:
ll
total 56
-rw-r--r-- 1 root root 32121 Jan 24 15:04 tcpdump.io.2019-01-24_145403.pcap.gz
-rw-r--r-- 1 root root 54 Jan 24 15:04 tcpdump.io.2019-01-24_150403.pcap.gz
-rw-r--r-- 1 root root 54 Jan 24 15:14 tcpdump.io.2019-01-24_151407.pcap.gz
-rw-r--r-- 1 root root 54 Jan 24 15:24 tcpdump.io.2019-01-24_152407.pcap.gz
-rw-r--r-- 1 root root 54 Jan 24 15:34 tcpdump.io.2019-01-24_153408.pcap.gz
-rw-r--r-- 1 root root 0 Jan 24 14:54 tcpdump.statistics
-rwxrwxrwx 1 root root 512 Jan 24 14:53 tcpdump_IO.sh
-rwxrwxrwx 1 root root 193 Jan 23 17:23 tcpdump_postproc.sh
Unzipping such a 54-byte-zipfile results in-rw-r--r-- 1 root root 0 Jan 24 15:04 tcpdump.io.2019-01-24_150403.pcap
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
A find will get anything indeed including the new next file...
Then again why use find...? $1 contains the correct name that just has been produced...
So: post-rotate.sh
Should be sufficient. probably running tcpdump with "-z $( which gzip)" should work as well.
From the tcpdump manpage:
-z postrotate-command
Used in conjunction with the -C or -G options, this will make tcpdump run " postrotate-command file " where file is the savefile being closed after each rotation.
For example, specifying -z gzip or -z bzip2 will compress each savefile using gzip or bzip2.
Note that tcpdump will run the command in parallel to the capture, using the lowest priority so that this doesn't disturb the capture process.
And in case you would like to use a command that itself takes flags or different arguments, you can always write a shell script that will take the savefile name as
the only argument, make the flags & arguments arrangements and execute the command that you want.
Then again why use find...? $1 contains the correct name that just has been produced...
So: post-rotate.sh
#!/bin/bash
gzip $1
Should be sufficient. probably running tcpdump with "-z $( which gzip)" should work as well.
From the tcpdump manpage:
-z postrotate-command
Used in conjunction with the -C or -G options, this will make tcpdump run " postrotate-command file " where file is the savefile being closed after each rotation.
For example, specifying -z gzip or -z bzip2 will compress each savefile using gzip or bzip2.
Note that tcpdump will run the command in parallel to the capture, using the lowest priority so that this doesn't disturb the capture process.
And in case you would like to use a command that itself takes flags or different arguments, you can always write a shell script that will take the savefile name as
the only argument, make the flags & arguments arrangements and execute the command that you want.
ASKER
Yup - you're right.
In the meantime I've changed the first "find" command to "gzip $1" in the postprocessing script.
I still use a postprocessing script because the dump needs to run a long time and the length of the backlog should be limited (the second find command ....). For convenience I shove tcpdump in the background now, too.
Thanks for mentioning that ...
In the meantime I've changed the first "find" command to "gzip $1" in the postprocessing script.
I still use a postprocessing script because the dump needs to run a long time and the length of the backlog should be limited (the second find command ....). For convenience I shove tcpdump in the background now, too.
Thanks for mentioning that ...
ASKER
Open in new window