0

Ok so I've just recently started studying network security, and had no knowledge of linux before doing so. I was trying to write a script that will basically do what the GUI in wireshark does when you follow tcp streams and then export the objects. I have pretty much no background in coding whatsoever and I was wondering the best format to do this in. Everything worked perfectly but then I decided to add a function to test the output against the original with md5sum. I can't get it to work.

function testScript {

    if [[ $test == "yes" ]]; then
            echo "Type original file path: ";
            read ogfpath;
            md5sum "$fpath" "$ogfpath" > print
    else
    echo "Goodbye"

fi
}

echo -n 'Type stream number and press ENTER: '
read stream

echo -n 'Type pcap path and press ENTER: '
read pcap

echo -n 'Type magic number and press ENTER: '
read mnum

echo -n 'Type new file path and press ENTER: '
read fpath

tshark -2 -q -z follow,tcp,raw,$stream -r $pcap | tr '\n' ' ' | sed 's\ \\g'      | grep -oP "(?<="$mnum").+" | sed "s/^/"$mnum"/g" | xxd -r -p > $fpath

echo -n 'Do you want to test the program (y/n)? :'
read test

testScript
  • `$test` in the function is a [local variable](http://tldp.org/LDP/abs/html/localvar.html). [See here](http://stackoverflow.com/questions/6212219/passing-parameters-to-a-bash-function) to pass parameters to the function. – miken32 Dec 14 '15 at 23:15
  • Also note your quotes are problematic here: `grep -oP "(?<="$mnum").+"` and here: sed `"s/^/"$mnum"/g"`. You'll want to escape the inner ones. – miken32 Dec 14 '15 at 23:20

1 Answers1

1

The problem I see here is that your $test variable is local, only accessible to your function from the inside, in other words, unless it's defined inside the function, it doesn't exist there at all.

One easy way to get around this is to pass parameters to the function, which is very easy in bash. For example:

function test {
    if [ "$1" == "yes" ]; then
        echo "True!"
    else
        echo "False!"
    fi
}

test "yes"

In this example, the parameter passed to the function "test" is "yes", which is accessed inside the function through the variable $1. More parameters can be passed to the function and accessed sequentially, $2 $3, etc. In your case, your function would have to be called like this:

testScript $test

And the if statement inside the function would have to look like this:

if [[ $1 == "yes" ]]; then
Skyler
  • 186
  • 1
  • 6
  • You should also avoid shadowing the `test` built-in command. Call your function something else. – tripleee Dec 15 '15 at 07:36
  • Thanks so much for the answer. I had a feeling the function wouldn't recognize the variable, but I had no idea how to go about doing it. So I'm not too sure if it worked because now when I run the program it gets all the way through asking me to input the original file path. I do, and then it exits. I am trying to get it to print out the md5 hashes on screen and then exit. – Ben Kaufman Dec 16 '15 at 00:28