so I have read about 10 answers and everyone seems to suggest ideas which for some reason don't work.
i am trying to execute a simple command line which is "svn update" but it is not working and it returns NULL
so i have tried trial and error the way and for now this is what i can say;
i have tried several commands like
<?php
exec ("cmd /c ping 127.0.0.1 -n 1 > results.txt ");
?>
and
<?php
exec ("cmd /c chdir > results.txt ");
?>
and both work.. infact chdir says the exact position where the php file executing the line is stored on the pc..
so the problem now is, why do some commands like this:
<?php
exec ("cmd /c dir > results.txt ");
?>
don't work? this results and empty value even though inside the folder i have several files and directories.
and why if i use the command prompt to move into the folder where the php file is store and type svn update it works and doing
<?php
exec ("cmd /c svn update > results.txt ");
?>
return a NULL?
any help is really appreciated.
it feels like i have some restrictions dued to the configuration setup because when i try in local using apache i can get most of the commands to work (shell_exec, system, exec, even without the cmd /c)
Ok.
i have managed to solve the issue..
this is what i did:
first check exactly what username is running for the specific website.. to do so do:
<?php
$out = array();
exec('cmd /c whoami 2>&1',$out,$exitcode);
echo "<br />EXEC: ( exitcode : $exitcode )";
echo "<hr /><pre>";
print_r($out);
echo "</pre>";
?>
this will return the computername followed by the username..
now on the computer running the webserver run
control userpasswords2
and give administrator powers to the username whoami said
this will allow you to finally run any command you want using exec or system_exec
on the other hand continuing with my SVN command i found out that I had another problem which is that when you run it, it will look for the config file which is under the administrator account and will give an error saying:
svn: E125001: Can't determine the user's config path
to solve this issue you simply have to specify in the command the config_dir by doing this:
exec('cmd /c svn update --config-dir C:\Users\Administrator\AppData\Roaming\Subversion C:\\inetpub\\vhosts\\websitename\\httpdocs\\folder 2>&1',$out,$exitcode);
hope this helps others which are having problems like the ones i had!
This is likely a system user permissions issue. I tried your example:
<?php exec ("cmd /c dir > results.txt "); ?>
On my Windows7 with Xampp installed and it worked perfectly fine. However with IIS the "user" may not have permissions to the directory, off the top of my head I think it may be the system user IIS-IUSR or something like that.
Here is a link that might help with user permissions for IIS: http://www.iis.net/learn/get-started/planning-for-security/understanding-built-in-user-and-group-accounts-in-iis
Okay, based on your answers, I think you should try this:
Execute your command using the 'svn.exe' executable (replace the [[reposity location]]).
It is possible that your client has another svn.exe location, but you will have to figure that out yourself :)
exec('cmd /c "c:\\Program Files\\TortoiseSVN\\bin\\svn.exe" up "[[repository location]]"');
What happends now?
There is also a second parameter in exec, maybe you should also take a look at that one.
The only thing that helped me was, complete routes:
$template_file = "C:/archivos/archivos.tex"
$cmd = sprintf("C:/texlive/2020/bin/win32/pdflatex.exe " .$template_file );
$result = exec($cmd, $output, $a);
Related
I am running PHP and Apache. My program is as follows:
<?php
echo shell_exec("ssh");
?>
It works from the command line as php script.php but when visiting the web browser 127.0.0.1/script.php it returns an empty result.
Any ideas why this may be happening?
Edit 1:
I tried running ssh as the www-data user through command line. This worked fine.
Edit 2:
I tried running sshpass instead of ssh (same location, same permissions) and it works but ssh does not.
Mentioned in kamermans's note, the help message of ssh is output to the stderr stream, so try
echo shell_exec('ssh 2>&1');
I've been unable to run php scripts that I need to use to start and stop webcam services that run on the local machine with the scripts. I can find nothing in the logs to indicate why the script doesn't' work.
I confess to being severely handicapped regarding PHP, especially server-side scripting.
The environment is Debian Jesse running Nginx with all required SSH and PHP modules installed
I have added www-data to the sudoers file with:
www-data ALL=(ALL) NOPASSWD: /var/www/html/start_webcam.sh
Enabled the $PATH environment for www-data at:
/etc/php5/fpm/pool.d/www.conf
The shell script resides in the .../html directory and runs from the terminal with no issues.
This is the code for both the php and shell scripts:
start_webcam.php:
<?php
echo exec('sudo bash /var/www/html/aspirebox/start_webcam.sh 2>&1, $output');
print_r($output);
?>
The $output and print_r stuff is there because it was the last thing I tried based on a post I found out here somewhere.
start_webcam.sh
#!/bin/bash
service motion start
Thanks in advance to anyone out here that has a clue. After 2 days of wrestling with this, I am sure that I do not.
according to Passing Variables to shell_exec()? you should change your code like this:
<?php
$output = exec('/var/www/html/aspirebox/start_webcam.sh 2>&1 ');
print_r($output);
?>
and let your bash script execute as all (no need to sudo bash):
chmod a+x /var/www/html/aspirebox/start_webcam.sh
Thank you very much - that worked.
I worked through getting the path straight for the directory the shell script runs in, and the correct path to run "service".
All I have now is to figure out why I'm getting "Failed to start motion.service: Access denied"
I've given www-data permission to run the script without a password on sudoers, have to keep digging.
Thanks again!
so I have read about 10 answers and everyone seems to suggest ideas which for some reason don't work.
i am trying to execute a simple command line which is "svn update" but it is not working and it returns NULL
so i have tried trial and error the way and for now this is what i can say;
i have tried several commands like
<?php
exec ("cmd /c ping 127.0.0.1 -n 1 > results.txt ");
?>
and
<?php
exec ("cmd /c chdir > results.txt ");
?>
and both work.. infact chdir says the exact position where the php file executing the line is stored on the pc..
so the problem now is, why do some commands like this:
<?php
exec ("cmd /c dir > results.txt ");
?>
don't work? this results and empty value even though inside the folder i have several files and directories.
and why if i use the command prompt to move into the folder where the php file is store and type svn update it works and doing
<?php
exec ("cmd /c svn update > results.txt ");
?>
return a NULL?
any help is really appreciated.
it feels like i have some restrictions dued to the configuration setup because when i try in local using apache i can get most of the commands to work (shell_exec, system, exec, even without the cmd /c)
Ok.
i have managed to solve the issue..
this is what i did:
first check exactly what username is running for the specific website.. to do so do:
<?php
$out = array();
exec('cmd /c whoami 2>&1',$out,$exitcode);
echo "<br />EXEC: ( exitcode : $exitcode )";
echo "<hr /><pre>";
print_r($out);
echo "</pre>";
?>
this will return the computername followed by the username..
now on the computer running the webserver run
control userpasswords2
and give administrator powers to the username whoami said
this will allow you to finally run any command you want using exec or system_exec
on the other hand continuing with my SVN command i found out that I had another problem which is that when you run it, it will look for the config file which is under the administrator account and will give an error saying:
svn: E125001: Can't determine the user's config path
to solve this issue you simply have to specify in the command the config_dir by doing this:
exec('cmd /c svn update --config-dir C:\Users\Administrator\AppData\Roaming\Subversion C:\\inetpub\\vhosts\\websitename\\httpdocs\\folder 2>&1',$out,$exitcode);
hope this helps others which are having problems like the ones i had!
This is likely a system user permissions issue. I tried your example:
<?php exec ("cmd /c dir > results.txt "); ?>
On my Windows7 with Xampp installed and it worked perfectly fine. However with IIS the "user" may not have permissions to the directory, off the top of my head I think it may be the system user IIS-IUSR or something like that.
Here is a link that might help with user permissions for IIS: http://www.iis.net/learn/get-started/planning-for-security/understanding-built-in-user-and-group-accounts-in-iis
Okay, based on your answers, I think you should try this:
Execute your command using the 'svn.exe' executable (replace the [[reposity location]]).
It is possible that your client has another svn.exe location, but you will have to figure that out yourself :)
exec('cmd /c "c:\\Program Files\\TortoiseSVN\\bin\\svn.exe" up "[[repository location]]"');
What happends now?
There is also a second parameter in exec, maybe you should also take a look at that one.
The only thing that helped me was, complete routes:
$template_file = "C:/archivos/archivos.tex"
$cmd = sprintf("C:/texlive/2020/bin/win32/pdflatex.exe " .$template_file );
$result = exec($cmd, $output, $a);
I'm trying to execute a command through PHP with shell_exec. The PHP file is hosted by Apache on my Ubuntu server.
When I run this:
echo shell_exec("ps ax | grep nginx");
Then I get to see data. But when I run another command, for example:
echo shell_exec("cat /usr/local/nginx/config/nginx.config");
Then it's not showing anything at all. But when I copy that command and paste it in my terminal, then it executes fine.
My Apache server is running as user www-data. So I edited sudoers and added this line:
www-data ALL=(ALL:ALL) ALL
I know this is a security risk, but I wanted to make sure (for now) that www-data is able to execute all commands. But, for some reason I'm still not able to execute all commands with my PHP script.
Anyone any idea what to do?
have you read http://php.net/manual/en/function.shell-exec.php
There is quite a discussion in comments section. Top comment is:
If you're trying to run a command such as "gunzip -t" in shell_exec and getting an empty result, you might need to add 2>&1 to the end of the command, eg:
Won't always work:
echo shell_exec("gunzip -c -t $path_to_backup_file");
Should work:
echo shell_exec("gunzip -c -t $path_to_backup_file 2>&1");
In the above example, a line break at the beginning of the gunzip output seemed to prevent shell_exec printing anything else. Hope this saves someone else an hour or two.
echo shell_exec("sudo cat /usr/local/nginx/config/nginx.config");
Try that.
I don't know what the deal is here…
So I want to run an applescript: sudo osascript myscript.scpt
This works fine in the terminal, but not when I execute it via PHP's exec(); nothing happens. The console says
no tty present and no askpass program specified ; TTY=unknown ; …
I did my research, and it seems I'm missing the password for the sudo command. I tried a couple different ways to get around this, including:
writing %admin ALL=(ALL) ALL in /etc/sudoers
and proc_open() instead of exec()
none of which seem to be working, consequently driving me CrAzY!
So basically, is there a clear-cut way to get PHP to execute a simple terminal command?
EDIT: to clarify, myscript.scpt is a simple appleScript that changes the onscreen UI (for a larger project). In theory, simply osascript myscript.scpt should be enough, however the sudo is for some reason necessary to invoke some response from the system. If the sudo could be somehow eliminated, I don't think I would be having this permissions problem.
It sounds like you need to set up passwordless sudo. Try:
%admin ALL=(ALL) NOPASSWD: osascript myscript.scpt
Also comment out the following line (in /etc/sudoers via visudo), if it is there:
Defaults requiretty
I think you can bring specific access to user and command with visudo something like this:
nobody ALL = NOPASSWD: /path/to/osascript myscript.scpt
and with php:
#exec("sudo /path/to/osascript myscript.scpt ");
supposing nobody user is running apache.
php: the bash console is created, and it executes 1st script, which call sudo to the second one, see below:
$dev = $_GET['device'];
$cmd = '/bin/bash /home/www/start.bash '.$dev;
echo $cmd;
shell_exec($cmd);
/home/www/start.bash
#!/bin/bash
/usr/bin/sudo /home/www/myMount.bash $1
myMount.bash:
#!/bin/bash
function error_exit
{
echo "Wrong parameter" 1>&2
exit 1
}
..........
oc, you want to run script from root level without root privileges, to do that create and modify the /etc/sudoers.d/mount file:
www-data ALL=(ALL:ALL) NOPASSWD:/home/www/myMount.bash
dont forget to chmod:
sudo chmod 0440 /etc/sudoers.d/mount
I recently published a project that allows PHP to obtain and interact with a real Bash shell. Get it here: https://github.com/merlinthemagic/MTS
The shell has a pty (pseudo terminal device, same as you would have in i.e. a ssh session), and you can get the shell as root if desired. Not sure you need root to execute your script, but given you mention sudo it is likely.
After downloading you would simply use the following code:
$shell = \MTS\Factories::getDevices()->getLocalHost()->getShell('bash', true);
$return1 = $shell->exeCmd('/path/to/osascript myscript.scpt');
Run sudo visudo command then set -%sudo ALL=(ALL:ALL) to %sudo ALL=(ALL:ALL) NOPASSWD: ALL it will work.
I had a similar situation trying to exec() a backend command and also getting no tty present and no askpass program specified in the web server error log. Original (bad) code:
$output = array();
$return_var = 0;
exec('sudo my_command', $output, $return_var);
A bash wrapper solved this issue, such as:
$output = array();
$return_var = 0;
exec('sudo bash -c "my_command"', $output, $return_var);
Not sure if this will work in every case. Also, be sure to apply the appropriate quoting/escaping rules on my_command portion.
The best secure method is to use the crontab. ie Save all your commands in a database say, mysql table and create a cronjob to read these mysql entreis and execute via exec() or shell_exec(). Please read this link for more detailed information.
killProcess.php
I think directly calling a sudo command might be difficult because you are setting up the whole server to work without a password.
Perhaps as an alternative you could setup a CRONjob as root and monitor a flag file. Once the flag file exists it will run the osascript myscript.scpt and then delete the flag file.
This way you will keep SUDO secure from a config point of view and the server safer. To run the script you just need to touch the flag file from PHP.
It would of course introduce a delay of however many minutes you running the CRON job. It would also mean that you would have to redirect the output to a file and have a async monitor of the output, but it will depend on your application if this is a problem or not.
But it is an alternative that might protect the server.