PhoenixBlue PhoenixBlue - 1 month ago 8
Linux Question

Executing a bash script via php

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?

Answer

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.