I’ve got a PHP script that needs to run the .sh file using shell_exec
echo shell_exec('sh /var/www/html/daloradius/start.sh > /dev/null 2>/dev/null &');
I just dump it into background. This is my start.sh
sudo tcpdump port 1812 -w testing.pcap
we know that tcpdump always listen all the time, I tried to resolve this (stop the tcpdump process) with the button that triggering another shell_exec which is stop.sh
pid=$(ps aux | grep "sudo tcpdump" | head -1 | cut -d '.' -f 1 | cut -d ' ' -f 7) sudo kill $pid
Stop.sh is doing fine when I tested it in cli, but when I click the button that triggering start.sh and I tried to stop it with the button that triggering stop.sh it doesn’t work. The tcpdump won’t stop, but when I try to stop it in cli using stop.sh it’s work well. Can anybody gimme solution to force stop the tcpdump things? Thank you
Advertisement
Answer
You are trying to use bash when you should be orchestrating the process from php.
Here, we get the PID of the command and kill it from PHP. Replace the sleep statement with whatever code you have.
<?php # Script must be run with sudo to start tcpdump # Be security conscious when running ANY code here $pcap_file = 'testing.pcap'; $filter = 'port 1812' $command = "tcpdump $filter -w $pcap_file" . ' > /dev/null 2>&1 & echo $!;'; $pid = (int)shell_exec($command); echo "[INFO] $pid tcpdump: Writing to $pcap_filen"; # Some important code. Using sleep as a stand-in. shell_exec("sleep 5"); echo "[INFO] $pid tcpdump: Ending capturen"; shell_exec("kill -9 $pid");
Please note that tcpdump has the -c
option to stop ofter n
packets received and you can rotate files with -G
. You may want to read up on tcpdump’s manpage to get the most out of it.