perlnewb perlnewb - 3 months ago 12
Linux Question

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

Answer

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