How to execute a simple Linux cmdline with PHP -


i want execute simple commands via network , process results php.

environment: linux mint 18 cinnamon 64 bit vers. 3.0.6 (linux kernel 4.4.0-21-generic)

opt/lampp/htdocs/cmd.php

<?php   //error_reporting(0);     error_reporting(e_all ^ e_notice);    // execute cmdline   $output = array();   exec('/usr/bin/sudo /var/sudowebscript.sh cmd=ifconfig', $output, $return_var);     echo $return_var;    $response = $_get["callback"]."(".json_encode($output).")";   echo $response; ?> 

/opt/lampp/etc/php.ini

; safe mode ; http://php.net/safe-mode safe_mode=off 

/var/sudowebscript.sh

#!/bin/bash # # sudo web script allowing user www-data run commands root privilegs  case "$1" in     cmd=iwconfig)        /sbin/iwconfig     ;;     cmd=ifconfig)        /sbin/ifconfig     ;;          *) echo "error: invalid parameter: $1 (for $0)"; exit 1 ;; esac  exit 0 

sudo visudo entry

# allow members of group sudo execute command %sudo   all=(all:all)  www-data all=(all) nopasswd: /var/sudowebscript.sh 

permissions

-rwxrwxrwx 1 gerhard gerhard 274 nov  7 17:03 /var/sudowebscript.sh 

user grouping (not sure if relevant ...)

gerhard@gerhard-thinkpad-t400 ~ $ groups root root : root gerhard@gerhard-thinkpad-t400 ~ $ groups gerhard gerhard : gerhard adm cdrom sudo dip www-data plugdev lpadmin sambashare gerhard@gerhard-thinkpad-t400 ~ $ groups www-data www-data : www-data 

exec statement in cmd.php returns rc = 1 , nothing happens

i guess missing ...

just noticed works without sudo (rc= 0 , $output populated)

exec('/var/sudowebscript.sh cmd=ifconfig', $output, $return_var); 

but original intention execute cmd's requiring root privileges. (the ifconfig simple example here) therefore .sh , keeping security in mind ...

maybe interesting. setup outlined above sudo works perfect on debian (raspberry) system. rebooting.

on debian must have setup, forgot here. ideas ?


Comments

Popular posts from this blog

php - How to add and update images or image url in Volusion using Volusion API -

Laravel mail error `Swift_TransportException in StreamBuffer.php line 269: Connection could not be established with host smtp.gmail.com [ #0]` -

C#.NET Oracle.ManagedDataAccess ConfigSchema.xsd -