CodingNovice
CodingNovice

Reputation: 103

Perl Script to Grep Directory For String and Print

I would like to create a perl or bash script that will read keyboard input and assign a variable, perform a fixed string grep recursively within the current directory filled with Snort logs, and then automatically tcpdump the matched files, grep its output, and print the specified lines to the terminal. Does anyone have a good idea of how this should work?

Here is an example of the methodology I want from the script:

step 1: Read keyboard input and assign it to variable named string.

step 2 command: grep -Fr "$string"

step 2 output: snort.log.1470609906 matches

step 3 command: tcpdump -r snort.log.1470609906 | grep -F "$string" C-10

step 3 output:

Snort log

Upvotes: 1

Views: 410

Answers (2)

Ed Morton
Ed Morton

Reputation: 203413

idk about perl but you can do it easily enough just in shell:

str="google.com"
find . -type f -name 'snort.log.*' -exec grep -FlZ "$str" {} + |
xargs -0 -I {} sh -c 'tcpdump -r "{}" | grep -F '"$str"' -C10'

Upvotes: 0

redneb
redneb

Reputation: 23850

Here's some bash code that does that:

s="google.com"
grep -Frl "$s" | \
    while IFS= read -r x; do
        tcpdump -r "$x" | grep -F "$s" -C10
    done

Upvotes: 2

Related Questions