I created a php file as shown below.
<?php
$ar = 4600;
$az = "redshift -O ".$ar;
echo "Command executed: ";
echo $az;
system($az,$status); // It is not working on browser but working on terminal.
system("ls",$asd); // It is working on both browser and terminal.
if($status == NULL) {
echo "<h3>Operation not successful</h3>";
}
else
echo "<h3>Temperature Succesfully set to ".$ar."</h3>";
?>
Now, the matter is that
when i am running this file on terminal using command
php file.php
the 'ls' command is getting executed and 'redshift -O 4600' also getting executed.
But when i am executing this file on browser using url.
127.0.0.1/file.php
Only 'ls' command is getting executed. 'redshift -O 4600' is not getting executed. Is there some way through which i can execute such programs.
I have also tried other functions like exec etc.
It would not work as you are setting up your server at /var/www/html location which is used by public to access your computer through localhost. And since in localhost there is no such thing as redshift so it would run nothing and the operation would be executed and you will see no after effects. So, to run the system's command on localhost, you must run the server on any folder other than /var/www/html. So, to run the server on any other location, just go to that location and run
php -S localhost:8080.
Related
I am using a php script on my apache/ubuntu server to call a bash script that triggers an application taking a python script as an argument (IDAPro).
PHP Code
chdir('/var/www/dashboard/team/static/sql');
$output = exec('sudo -u rohan ./start.sh');
Now, the above code works fine if I run the PHP file from the terminal - but only if I run it as the root user. Needless to say, if I execute the bash file directly it runs too.
But when I run the PHP file on the browser, it doesn't work and I get the following error in the apache error log:
QXcbConnection: Could not connect to display
Aborted
I understand that Apache/php runs as 'www-data' user (used the 'whoami' to verify), and that is why I have the sudo in my exec. I have tweaked and tinkered the permissions for both users to no avail. When I run the php file from the terminal as the 'www-data' user, it throws no error but does not do anything except display the random echo tags I at the start and end of the script to debug it.
I am a linux novice, so any help is greatly appreciated.
Okay, I finally managed to solve it.
The issue is not with the permissions, but it is with the environment variables.
I had to include the following line in my bash script
export DISPLAY=':0.0'
Note that setting the variable in the terminal and running the script does not work. The line needs to be inside the script.
I assume this is because the DISPLAY variable is not set if you run the script as any user other than root, which is what happens in case of Apache/PHP where the script is executed as the 'www-data' user.
perhaps you could use something like the following at the top of your script:
if [ "$(id -un)" != "rohan" ]; then
exec sudo -u rohan $0 "$#"
fi
export XAUTHORITY=/home/rohan/.Xauthority
export DISPLAY=:0
I am unable to execute a source command in linux using php.All other commands are working except this one. I need to execute the following command.
source /root/Envs/ate/bin/activate
This activates the ate-Automatic Test Equipment.Once I activate it then I need to run a python script as the script accesses the remote server.
I am able to manually run it but I am creating a tool which will automatically do it.
<?php
exec("source /root/Envs/ate/bin/activate", $output, $return);
echo "Command returned $return, and output:\n";
echo exec("python box_upgrade-pradeepa.py");
?>
The above commands returns 1 which means there is an error.But I am not sure how to run the 'source command'. The python script will run only if the source command is successful.(the python command is correct as I replaced hello.py and it ran fine.)
Could you pls help me as I am really stuck for a week?
Thanks a lot..
I found out the error. Since I am doing it using php (for a web tool) the user is Apache. 'Apache' user is unable to access the script in root folder. Moving it to another directory, I am able to run the script fine.
Thanks all..
I am have a PHP script called process_item.php that can be run from terminal. I want to create a web script that can run the same file using exec(). My web script(process_item_online.php) looks like below:
ini_set("display_errors",true);
error_reporting(E_ALL);
$item = $_GET['item'];
echo "Received request for item $item<br>";
$cmd = "/usr/bin/php /var/www/html/process_item.php $item";
exec($cmd);
echo "Processed using command - $cmd";
The script process_item.php has 777 permissions. When I run the above script in browser, I get both these lines printed correctly:
Received request for item 10023444AJK
Executed command - /usr/bin/php /var/www/html/process_item.php 10023444AJK
However nothing else happens. As in, the database is not updated, the logs are not written etc. I do not get any error on the browser either. I am sure this is a permission issue since it works fine from the terminal. Can anyone give me any pointers on how I can make this work from the browser?
$cmdOutput = shell_exec("perl run_single_test/hello.pl");
echo "the command output = $cmdOutput";
This causes the file hello.pl to execute and print "hello world back to page". But
shell_exec("perl run_single_test/test_single_run.pl -s \"$testSuiteName\" -t \"$testName\" -i $time");
does not get executed. I echoed the command to screen and ran it on terminal, the Perl script executed perfectly. test_single_run.pl creates a log file and copies a few files.
What am I missing?
Finally figured out the answer.
The test_single_run.pl was modifying a few files in other directories.
I got to know that apache runs the perl script as a different user . In my case it was wwwrun and thus lacked the permissions to operate on other files.
Changed the configuration file uid.conf to make apache run the perl script as my user. And it worked
I'm trying to execute a command as su from php.
When I put the command in terminal or with php /var/www/script.php it works perfect, but when I open script.php through browser it returns - sudo: no tty present and no askpass program specified
Thanks.
According to this:
ssh does not allocate a tty by default when running a remote command.
Without a tty, sudo cannot disable echo when prompting for a password.
You can use ssh's "-t" option to force it to allocate a tty.
Alternately, if you do not mind your password being echoed to the
screen, you can use the "visiblepw" sudoers option to allow this.
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
You can get a real bash shell as root. You can then either trigger your php script or execute the commands directly in bash.
After downloading you would simply use the following code:
$shell = \MTS\Factories::getDevices()->getLocalHost()->getShell('bash', true);
$return1 = $shell->exeCmd('yourFirstCommand');
$return2 = $shell->exeCmd('yourSecondCommand');
//the return will be a string containing the return of the script
echo $return1;
echo $return2;
//or if you want to trigger your script as root
$return3 = $shell->exeCmd('php /my/script.php');
echo $return3;
//If your script contains code to make an SSH connection to another server
//there is a much easier way to do that using the project:
$shell = \MTS\Factories::getDevices()->getRemoteHost('ip_address')->getShellBySsh('username', 'secretPassword');