简体   繁体   中英

Creating a tshark bash script to export objects

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM