gpt4 book ai didi

linux - 创建一个 tshark bash 脚本来导出对象

转载 作者:太空宇宙 更新时间:2023-11-04 10:42:53 24 4
gpt4 key购买 nike

好的,所以我最近才开始学习网络安全,在此之前我对 Linux 一无所知。我试图编写一个脚本,该脚本基本上会执行 wireshark 中的 GUI 在您遵循 tcp 流然后导出对象时所做的事情。我几乎没有任何编码背景,我想知道执行此操作的最佳格式。一切都很完美,但后来我决定添加一个函数来测试输出与 md5sum 的原始输出。我无法让它工作。

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 变量是本地的,只能从你的函数内部访问,换句话说,除非它是在函数内部定义的,否则它根本不存在。

解决此问题的一种简单方法是将参数传递给函数,这在 bash 中非常容易。例如:

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

test "yes"

在这个例子中,传递给函数“test”的参数是“yes”,在函数内部通过变量$1访问。可以将更多参数传递给函数并按顺序访问,$2 $3 等。在您的情况下,您的函数必须像这样调用:

testScript $test

函数内的 if 语句必须如下所示:

if [[ $1 == "yes" ]]; then

关于linux - 创建一个 tshark bash 脚本来导出对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34278004/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com