2

我正在尝试在 Ubuntu 中编写一个 bash 脚本以在 android 模拟器上安装应用程序,使用“monkey”向应用程序发送随机命令并使用 tcpdump 捕获所有数据。代码:

#!/bin/bash

#store all apks files in array
shopt -s nullglob
packageArray=(*.apk)


function getPackageName()
{
    myResult= aapt dump badging $1 | grep package | awk '{print $2}' | sed   s/name=//g | sed s/\'//g
}

#loop through array installing, testing and capturing data, and uninstalling
for i in "${packageArray[@]}";
do
    :
    myResult=$(getPackageName "$i")

echo "------------------INSTALLING-----------------"

sudo adb install $i
echo "*****************INSTALLED****************************"

echo "*****************TESTING****************************"

#-------THESE COMMANDS ARE THE TROUBLE-------

(sudo -i xterm -e "tcpdump src 10.0.2.11 -vvv >   /home/seed/Documents/autoTcpLogs/$myResult.pcap" &
sudo -i xterm -e "adb shell monkey -p $myResult -v 500")
echo "------------------DONE TESTING-----------------"

sudo adb uninstall $myResult
echo "*****************PACKAGE UNINSTALLED****************************"

done

问题:一旦猴子完成发送 500 个随机命令,我需要一个好方法来关闭 tcpdump。我已经尝试以几种不同的方式使用 KILL 命令,但它似乎并没有奏效。

4

1 回答 1

1

考虑以下示例:

#!/bin/bash

sudo -- tcpdump > /dev/null &
SUDO_TCPDUMP_PID=$!
echo "Waiting 3 seconds"
sleep 3;
echo "3 seconds elapsed"
sudo -- setsid kill -TERM "${SUDO_TCPDUMP_PID}"

编辑(阅读问题评论):我们需要使用setsid来强制从不同的会话发送信号,否则sudo不会中继信号(参见sudo手册页)。

The PID of sudo tcpdump ... (the sudo pid in this case) will be stored in the SUDO_TCPDUMP_PID variable, which will be used as kill argument:

$ ./tcp.sh
Waiting 3 seconds
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on wlp2s0, link-type EN10MB (Ethernet), capture size 262144 bytes
3 seconds elapsed
0 packets captured
2 packets received by filter
0 packets dropped by kernel
$
于 2016-08-02T20:40:12.683 回答