Good day all, I m trying to implement a web interface that will operate my wireless network.
One of the operations is to configure my card into monitor mode. pretty simple, if you run this command:
bash prepareCard.sh wlan0
and the script prepareCard.sh is as follows:
#! /bin/bash
IFACE=$1
ifconfig $IFACE down
iwconfig $IFACE mode monitor
ifconfig $IFACE up
Now I want to execute this script via a php script:
$cmd = shell_exec("bash prepareCard.sh wlan0");
when I check if the card has been set to monitor mode, nothing! it's still in management mode!!
Can you please tell me where did I go wrong?
Assuming the webserver user that is running the script does not have sufficient permissions, you can try this way to fix it:
Use command visudo to edit /etc/sudoers and add this line:
ALL ALL=(root) NOPASSWD: /absolute/path/prepareCard.sh
Make sure to set permissions 700 to the script, so no one else can edit it. Then execute your script with sudo like this:
$cmd = shell_exec("sudo /absolute/path/prepareCard.sh wlan0");
That should execute the script as root without a need to enter a password.
One good way to debug BASH scripts is to set debug mode on (either by passing an -x flag to bash in your shell_exec call, by running set -x, or in the shebang line #!/bin/bash -x). This shows you what's going on during execution of the bash script. In your case, I suggest the first case, since you don't know if the script is even being loaded in the first place.
$cmd = shell_exec("bash -x prepareCard.sh wlan0");
After that, you should have more in your $cmd variable.
Related
So, guys, I have the following code:
<?php
$url = "http://example.com/filetorun.php";
$time = date('g:i A d.m.Y', $res['sometime']);
echo `echo "wget --spider {$url}" | at -M {$time}`;
?>
The main idea is to wget some url at the specific time to perform some useful operations for me.
What happens in the terminal?
When I run this bash code. It works well giving me the output like this:
warning: commands will be executed using /bin/sh
job 59 at Mon Jan 13 17:12:00 2020
What happens in /var/log/apache2/error.log when I run my php script?
It gets this output.
What's about atq?
I can see the job there only when I create it via the Terminal. When I create It from the php-script, I can not do that.
What have I tried to do?
I added users to /etc/at.allow and deleted them from /etc/at.deny
Not a lot of detail to go on, but I direct your attention to this requirement of at:
The value of the SHELL environment variable at the time of at invocation will determine which shell is used to execute the at job commands. If SHELL is unset when at is invoked, the user's login shell will be used; otherwise, if SHELL is set when at is invoked, it must contain the path of a shell interpreter executable that will be used to run the commands at the specified time.
Does your web server user have a login shell defined? getent passwd or grep /etc/passwd to determine. Regardless, I suggest setting the shell to use in the command itself, to prevent issues where someone chnahes the default shell of the invoking user.
If this is not the issue, we probably need more detail.
I'm trying to execute a linux command in PHP, here is my sample code:
$command = "last -F";
$o = shell_exec($command);
print_r($o);
Most of the Linux commands gives me an output, but for the Last -F command, I have no output. Why is it so?
Try This Explaination. Your issue MAY be that the last line of last -F is a new-line. shell_exec() only returns the last line of the command, and therefore, if that line is empty, you get nothing, nada.
As an alternative, try exec(), this will allow you to capture the return value (success or failure of execution) as well as the entirety of the command's output. Check it out here
You are executing that command as web user (nobody or www-data), which is a limited privileged user.You have to execute that command as root. Unfortunately giving sudo permission or full permission to web user is really a bad idea. So I recommend make a cron or background script that execute last -F and write output to a file. You can read that file from your PHP script.
You can make a script runs in background like this.
#!/bin/bash
while [ true ]; do
last -F > /tmp/myfile
done
save the code as mycron.sh
chmod +x mycron.sh
mycron.sh &
Read the file /tmp/myfile from your PHP Program. It will give the exact output of that command.
purpose: use php to input commands directly into the minecraft server console
Im trying to use a php script (run from browser) to exec() a shell script. when i run the php from a terminal it works! But in the browser, nothing happens.
exec('sudo -u root sh /home/minecraft/whitelist-reload.sh', $out, $ret_val);
When running from terminal, i get a "array 0" but the browser gives me a "array 1"
what is the issue?
and once i run the shell, shouldn't everything after that work as if you were on a terminal?(does it matter what is inside of shell script?)
the shell has all rx permissions and is in the sudoers file as
www-data ALL = NOPASSWD: /home/minecraft/whitelist-reload.sh
The problem is, that you run the script on a terminal as a user that probably has the sudo rights, whereas the apache/webserver user doesn't, so the $ret_val (which is actually just a status code) is set to 1 (means error).
try var_dump($out); in both cases to see the results of your exec call. To do this kind of thing from the browser, you might want to look into proc_open and family, or have a script that is chmod'ed to 777, so the apache user can run it, too. Let that script then call the actual shell script and return it's output back. This is, however very dangerous, and should only be used for testing environments on your own machine. Never do this in production environments!
I have posted a couple of questions here, too that might prove informative:
interaction over ssh
opening a second shell, and load profile variables AND call another script
Turns out... after inputting www-data into the sudoers file, all i needed to do was take of the "-u root" after it
I would like to create a php script to execute a shell command and return its output. The server requires a private key. When I first decided to test this out I created this:
<?php
$command = "ls";
$output = shell_exec($command);
echo "<pre>$output</pre>";
?>
That worked just fine. But when I changed $command to the command I really wanted to run:
$command = "/etc/init.d/mycontrollerd status /etc/mycontrollerconfig";
it gave me this output:
You need root privileges to run this script
My guess is I need to use sudo. Of course that will require putting the pem file somewhere on the server. Assuming I do that, what exactly should $command be? Should I use shell_exec(), exec(), system() or something else?
It does not matter which php function you use to start the script - what lacks is the authorization of your user account.
Either use sudo (preconfigure the web server user to run the exact command without password via visudo, and prefix the command with sudo) or set up a setuid script that executes the command on itself.
What you really need to do is set your web server to run as a specific user (other than 'nobody' for example), or give that user permissions to what you want to execute.
See also: PHP shell_exec() and sudo: must be setuid root
I'm writing a class who let me access to recutils through PHP.
I have a 'database' file called books.rec in ../database/ and a script who runs my Recutils.php class.
My class simply launch system application with correct parameters.
But When I try to use recins with PHP's exec function, the command doesn't work will it work in command line.
This is the command that is executed by my script :
recins -f Title -v "Moi" -f Author -v "Moche" -f Location -v "loaned" -t Books ../database/books.rec
With PHP : Nothing, the record is not inserted (no error message at all too).
In terminal : OK, the command is well done and my record is inserted.
I also have a method to do a select operation using recsel and it works very well, will it use exactly the same file (and runs from exec too).
So, could someone explain me why the command don't work will another with the same file work ?
Thanks
PS : Further informations : http://www.gnu.org/software/recutils/
I would double check that you are running the command as the same user from the command line and your php script. That may be the problem. exec('whoami')
You said you had a script that starts your php script it should be the same user as that.
You might also want to running a simpler exec command to see if that will work first.
Other things to try:
Try checking stderr output exec('ls /tmp 2>&1', $out); This will redirect standard error to standard out so you get both.
Try using php's shell_exec() which will invoke a shell just like when you are running from the command line(eg. bash). shell_exec('ls /tmp 2>&1 >> /tmp/log') should even put all output into a log file.
I don't think this will help you but it is something to try if all else fails, set it as a background process and see if it completes. exec('nohup php process.php > process.out 2> process.err < /dev/null &'). The & will set the command to run in the background and let the script continue.
Good Luck
Is recins command accessible for PHP ? Also is path to books.rec correct ?
Try with absolute path.