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

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.

Related

shell_exec() tcpdump from 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?
?>

Executing rsync shell command from php file on web browser

Firstly, we wanna move the changes from one system to another system and for this, we have a shell script in synchfolders.sh file as follows
rsync -av --delete -e 'sshpass -p Wordpress#123 ssh -p 22' root#192.168.2.94:/var/www/html/prosync/wp-content/plugins/ /var/www/html/devsync/wp-content/plugins >> /var/www/html/devsync/wp-content/mysynclog.txt
and we want to execute this shell script in PHP file by shell_exec()
and while executing this PHP file from a browser other than rsync command, all are executing but the rsync is not executing. We have searched the stuff in SO and we got the link php exec() rsync ssh to remote server not working
as said here, we have tried the PHP file execution from the command line and works perfect but not through the browser. Why, Please let us know where we did a mistake. Thanks in advance
Enter the full path of rsync command:
/usr/bin/rsync -av --delete -e 'sshpass -p Wordpress#123 ssh -p 22' root#192.168.2.94:/var/www/html/prosync/wp-content/plugins/ /var/www/html/devsync/wp-content/plugins >> /var/www/html/devsync/wp-content/mysynclog.txt

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?

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.

execute bash script from php without waiting

I execute a bash script from php with shell_exec.
But the php script waits until the shell script is finished.
Can I somehow call the bash script without waiting.
Both:
exec
shell_exec
are waiting until the bash script is finished.
I'm running linux btw.
This has to work:
exec('/your/command /dev/null 2>/dev/null &');
when calling your bash script append & so it will run in the background
that's the easiest way if you don't need any output
shell_exec("/bin/bash /path/to/script.sh &");

Categories