I have around 7200 compressed .pcap files. Each is compressed into a separate .gz file. I need to look for a specific string in packet data details. I would like to write a command to do that. At the moment all I have is:
zcat 20230212*.pcap.gz | tcpdump -qns 0 -X | grep "specyfic string"
where 20230212*.pcap.gz is pattern for these 72000 files.
I know that problem is somewhere on tcpdump part. Sorry for my english.
Update
I tried
tcpdump -qns 0 -A -r filename.pcap | grep "string"
where filename is name of specyfic file, that contains string. It works, but I had to unzip this file. I cannot do it for all files. Also tried:
tcpdump -qns 0 -X -r filename.pcap | grep "string"
but this command cannot find string.
xargs zcat filename.pcap.gz | tcpdump -qns 0 -A -r | grep "string"
gives me: tcpdump: option requires an argument -- 'r'
The
-rflag needs to be given an argument to indicate what to read.An argument of
-means "read the standard input", which is what you want here, as you're piping the result ofzcatto it.So you want
You don't want
xargs, because, withit will:
zcat filename.pcap.gz {bunch of file names}- meaning that it will decompresss first filename.pcap.gz, followed by all of the files in that bunch, and write the decompressed contents of all those files as a single stream of raw bytes;which means that what tcpdump will see will look like a bunch of pcap-format files stuck together ("concatenated") into one. That will NOT look like a single pcap-format file to tcpdump; instead, it will look like the first pcap file, followed by a lot of stuff that will not look like valid pcap file contents, so tcpdump will probably print an error and give up.
(And other programs that read pcap-format files, such as tshark, will do the exact same thing. There's no magic flag or tool to fix that.)
What you should do, instead, is have a small shell script, such as
and, to look for a given string in one .pcap.gz file, do
where {path to script} is the path name of the script and {file name} is the pathname of the file.
To scan all the files, do
That is a loop that loops over all files that match 20230212*.pcap.gz and, for each of them, runs the script on the file, looking for the string, and sends the output of that entire loop to the file /tmp/output.
Note that /tmp/output will contain one line for every file, giving the name of the file. If you don't care which capture files contain the string, you can remove the
line from the script. If you do care which capture files contain the string, but you don't care what the exact text that matches is, you can have the script be
which tests whether the
grepcommand found the string and, if it did, prints a message. The-qflag causesgrepnot to write the matching text out, so the file doesn't have that extra information in it.That's because you didn't provide a
-rargument to tcpdump, which means that will capture network traffic from a network interface; because you also didn't specify a-iargument, which would specify an interface from which to capture, it will pick the first interface that shows up in the list it gets from the system, which happened to be bond0 on your system.You need to specify
-rto get tcpdump to read from a capture file.That command uses
-X, not-A, so it dumped out packet data in a format like this:There's no guarantee that the string will all fit on one line.