shell_exec() tcpdump from php - php

I am trying to write a script in php to run the following bash script using apache2+php7
#!/bin/bash
#cd /home/cwc/http/www/html/admin/web/
nowfile=$(date +"%Y%m%d-%H%M%S")
nohup tcpdump -w $nowfile.pcap -i enp2s0 >> /dev/null 2>&1 &
Now I understand I might have to use full paths
The above code works with a non sudo user using bash because I added a user the the pcap group.
I'm trying to figure out why this will not work with php
<?php
$command = "/pathtoscript/tcpdmp.sh
shell_exec($command); //not working?
?>

Related

exec(); or shell_exec(); performance issue

I'm trying to execute by php exec(); a bash file which runs a process based on curl looping script. The problem is I've tried these methods:
exec();
shell_exec();
sudo -S nice -n -20
sudo -i
> /dev/null 2>/dev/null & (achro)
and started process by PHP exec(); are slow, response from curl is milliseconds slower than if I run it manually on SSH console directly as a command.
I have it this way right now:
start.php run this:
echo shell_exec('echo "password" | sudo -S nice -n -20 ./new.sh > /dev/null 2>/dev/null &');
new.sh run this
sudo -i /home/scripts/source/do.bat > /dev/null 2>/dev/null &
and do.bat run this
cd /home/scripts/a/2
echo "" > output.txt
./check.bat & echo $! > check.pid
cd /home/scripts/a/6
echo "" > output.txt
./check.bat & echo $! > check.pid
When I execute directly ./do.bat at console, script runs pretty fast, curl responses are as I need and everything goes fine, output.txt is written with a decent output speed.
But when I try to run it with PHP exec(); curl responses are slow down, and does not work as I need. Also you will ask why I run new.sh with start.php instead runs directly do.bat with start.php? Because if I do it, I get an output response very weird with an -e wrote on text.
Somebody can help me to make it run at same speed as if I run manually on console as command?

exec () php, sh script runs partially

sorry for bad english..
i have php file like this:
<?php
exec(`sh /tmp/script.sh`);
echo "Work!";
?>
and this is the script:
#!/bin/bash
url="http://someweb.com/get.php?user=user&pass=pass";
wget -O /tmp/file.txt $url
sed -i 's/#Test_file/Ok_Test_file/' /tmp/file.txt
cp /tmp/file.txt /var/www/_client/personale/file.txt
Now when load file.php to the browser, the script works ,but only commands
wget and sed are performed , except cp which doesn't work..does not copy the file!
If i run the script to terminal manually (Debian 8) all cmd are executed...
Where is the problem?
Thanks.
Joele
PHP likely does not have permission to execute the command. Try using sudo to execute the command.

Running SQLCMD from bat file loops infinitely

I have the following SQLCMD which connects to a remote database executes a query ands saves it as a csv file.
sqlcmd -S tcp:0.0.0.0,1433 -U user -P pass -d mydb -W -w 999 -s "," -Q "SELECT * FROM dbo.CSVData" -o "C:\wamp\sqlcmd.csv"
This works fine when i run it manually from command prompt.
I need to run the sqlcmd command from php and i have tried several execution commands in php but none works. The only option is using .bat files and call them through php but when i do so the sqlcmd command runs continuously (for several hundred times) and quits. I have tried using /wait and exit but this makes it worse by opening hundreds and hundreds of command prompt windows and it freezes the system. Below is the code i tried. Please let me know what i am doing wrong. Thanks.
#echo off
echo Running sqlcmd
start /wait sqlcmd -S tcp:0.0.0.0,1433 -U user -P pass -d mydb -W -w 999 -s "," -Q "SELECT * FROM dbo.CSVData" -o "C:\wamp\sqlcmd.csv"
:exit
I found what the issue was. I named the .bat file as sqlcmd.bat and that caused the program to run infinitely. I renamed the .bat file and it is working perfectly. A stupid mistake. My bad.

Php exec ffmpeg not running in background

I am trying to start a ffmpeg process from a php script and I know it has been asked a lot of times but I tried many solutions and none of them seem to work, each time the php script never finishes unless I kill the ffmpeg process. At the moment I am using this script which indeed starts ffmpeg and writes info in the designated files but the php script is loading forever.
What am I missing?
$cmd = 'cd cache && ffmpeg -y -i "rtsp://stream" -r 20 -f image2 a%6d.jpg >/dev/null 2>/dev/null &';
exec(sprintf("%s > %s 2>&1 & echo $! >> %s", $cmd, 'log.txt', 'error.txt' . '.pid'));
A little more info: I am running FFMpeg 0.6.5, PHP 5.3.3 on CentOS 6.5
Thank you for your time!
You can use > /dev/null & instead of 2>&1 &
This will execute $cmd in the background without PHP waiting for it to finish.
Ref: http://php.net/manual/en/function.exec.php

How to execute remote SSH command in background using PHP exec()?

Is there a simple way to execute SSH commands in the background on remote machines from PHP without using ssh2_*? The PHP script is executed by the user from bash (no Apache involved), so it's not an issue of rights. I've tried doing this:
exec("ssh -f -o UnknownHostsFile=/dev/null -o StrictHostKeyChecking=no -i {$keyFile} {$user}#{$ip} {$remoteCommand} 2>&1 >/dev/null </dev/null");
For example:
exec("ssh -f -o UnknownHostsFile=/dev/null -o StrictHostKeyChecking=no -i /home/data/id_rsa user#192.168.0.19 '/home/user/script.sh ; exit' 2>&1 >/dev/null </dev/null");
All PHP variables have been escaped with escapeshellarg() and $remoteCommand is a bash script on the remote machine that sleeps for a few minutes and then starts executing some commands.
My problem is that if I execute that SSH command from bash, it gives control back to bash immediately. If I execute it from php using exec() it waits until the remote command executes. I've tried adding 2>&1 >/dev/null </dev/null after /home/user/script.sh, but the execution still doesn't return control to the PHP script.
I think you are missing an & at the end of your command for sending the execution to the background.

Categories