PhoenixBlue PhoenixBlue - 1 year ago 80
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 wlan0

and the script is as follows:

#! /bin/bash
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 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 Source

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/

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/ wlan0");

That should execute the script as root without a need to enter a password.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download