shell_exec on php - php

I am using the shell_exec command to send commands to my school unix server. I can do things like ls echo and things like that but anything that has to do with creating or deleting a file does not work. I figured I can just create a script for the command I want to perform and run the script with shell_exec, however this is a very tedious process is there any other way around this issue??
Thanks,

shell_exec() is a very bad way of executing commands just because leaving it open, you're leaving your server a whole lot more vulnerable if someone exploits your PHP code, or manages to upload a PHP file. Instead, if you can, you should use PHP functions like mkdir() and fopen($file, '(r|a|w)+) so your file is created if it doesn't exist. In fact, leaving shell_exec(), exec(), pass_thru() and the like in your code, any exploits can execute shell commands on your server.
Now, in relation to the original question, have you checked permissions on the files and directories you're attempting to work with? The user running your PHP needs to have permission to work with the files and directories you're attempting to access. If you're unsure, just chmod 777 any of your files and directories that you need to use in PHP.

Sounds like a permissions problem. Run whoami from within PHP and see what user it's running as. If your school is also using something like SELinux, then there's additional restrictions on process beyond just those set by user/group/mode. But that's a starting point.

Related

How can I run a cronjob for a php file properly inside of c-panel?

I am trying to run a cron-job that automates the cache rebuild function of Woo Search Box plugin.
The plugin has this dynamic cron command, that I set to run once a day at 1AM:
php /home/carit/public_html/index.php 16021417635f7ebe43c604a
I am well experienced with cron jobs inside of C-Panel but I can't figure out what prevents this cron from running. All in all, I have tried what the documentation suggests here, also tried usr/bin/php instead of php but it didn't make any difference.
If I run this cron-job inside my server's terminal, it works like a charm and the cache is being rebuilt. It only seems to not be working when I run it via cron-job.
Does anyone have any suggestions or ideas on why doesn't this cron-job works?
Please correct me in the comments in case I forgot to provide any key information about my problem,
Thanks.
It seems that some newer versions of CentOs or Ubuntu servers use either /usr/local/bin/php or simply /usr/bin/php. If you have ftp access to the server you want the cron to run in, make sure to check for the php file in both paths recommended above before trying to use the ordinary php before a cron job.

How to execute shell commands from plesk extension

i'm making an extension that use a php file in the 'public' directory as a webhook.
From this hook, I will need to run some shell commands (things like npm run build or similar). Is there some kind of api from plesk that let me do this? Or should I use a php function like exec or shell_exec?
I looked through the net, but in the end it looks like what I'm trying to accomplish is not doable. You can indeed use shell_exec to run shell commands, but the user used to run this commands is psaadm, which is a severely restricted user when operating inside a domain document folder. You can see the files, but not create or execute any script, which renders this function useless for what I wanted to do.
If you want to use a webhook tu run your npm build (or other scripts), you can always create a php file in the document root folder and execute your commands using shell_exec. When you call this file, it will execute the commands as the customer user, and will have the ability to run those scripts.
P.S. It might actually be accomplished if you change the permission for the psaadm user, but it's not something a customer which install an extension should do. Still, not sure if it will work for the domain document root folder of your customers.

SVN commit command line with path inside a variable in php

I need to create a web app which works with SVN command line. So I want to use a exec command with svn commit and a variable contains the path of the working copy. I tried this code but didn't work. Commit didn't take place.
<?php
$lpath="c:\a\svn\projectwc";
$msg="first commit";
exec("svn commit -m $msg $lpath");
?>
And yeah I added all the files inside already. I tried by replacing the lpath with path value and it worked.
Please help...
Your string expands to
svn commit -m first commit c:\a\svn\projectwc
Whereas you need to do
svn commit -m "first commit" c:\a\svn\projectwc
As the log message is more than one word it needs to be in quotes.
I think you should re-think what you are trying to achieve here. Constructing strings based on user input and passing them to exec is a very bad idea from a security perspective.
Also you tagged the post tortoise-svn - if you invoke TSVN from the command line it pops up the GUI which is definitely not appropriate server side.
Then you've got other considerations like:
How to log error messages when things go wrong
Does the Web server have sufficient access to the working copy to perform these operations
Is the invocation of "svn" even possible in your environment.
Whatever you are trying to do, it's probable there is some free software out there that does it already. svn has a webdav interface and an API implemented in many languages - this would be far safer than using exec

PHP exec to launch a cmd window with parameters

I have been trying to launch a cmd window using the php exec method. The idea is to launch cmd and get it to start running a file. My first problem was being able to rewrite the bat file on the PC when user changes the information such as their user information or what tools they would like to launch. Now that is no longer a problem. I was able to use php popen function to get passed that. From all the reading I have done I was able to determine that it is possible to launch cmd with an additional command telling it to launch the batch file. I just need a good example so I can wrap my head around it. If anybody can help I would really appreciate it.
Thanks in advance
Make sure the batch file is executable, then you can simply exec it:
exec('myscript.bat');
You shouldn't need to do it via cmd. If you want to for some reason, you will have to provide extra parameters just as you would on the command line:
exec('cmd /C myscript.bat');
http://ss64.com/nt/cmd.html
will show you how to provide other options to cmd (and explain the one above).
Have fun, let me know if you get any problems.

How to run a .php file over and over...cron perhaps?

I have a php file that calls exec() on a c++ exe.When the .exe is finishing running I need to run the php file again,repeat .
I am wondering what is the best way to do this? This .php file has no user interaction and needs to be completely automated.
EDIT:Found another solution that would kill the process if it is already running which will cover this issue well.See posts on this page.
http://php.net/manual/en/function.getmypid.php
Here's a simple Linux line that starts up the script in the background and uses the watch command to restart the script whenever it finishes running:
watch -n 0 php /path/to/script.php &
Once you've started it like this, you can use the ps command to list running processes and find the process ID for watch, and then use kill process_id to stop watch.
It's probably not best practice to run commands like this, but it's a quick and easy solution that doesn't require any special access privileges on the system (whereas using cron might), and doesn't involve editing your code (whereas a codeigniter solution will).
I haven't used codeigniter before but there seems to be a solution as described in the wiki.
Depending on how you can access the system (if you are admin or not) and depending on how you are planning to update the automated commands, IMHO, you could use both solution (Linux crontab or codeigniter cron script).

Categories