• tatzka

    (@tatzka)


    Hi,
    I have a a small wp site with a form. Once user clicks submit button the site processes a a few functions (all working) located within functions.php (ok, know this may not be best practice but did it for simplicity and didn’t want to write my own plug-in).

    Functions include:
    1) creation of unique username
    2) creation of a random password
    3) storing unique username into db

    I need to submit the random password to MQTT server using mosquitto_passed tool like this:
    /usr/bin/mosquitto_passwd -b /etc/mosquitto/passwd [uniqueusername] [randompassword]

    Both the WP server and the MQTT server reside within same machine (Raspbian).

    How do I execute such cmd line from within functions.php?? Or any other shell command?

    • This topic was modified 6 years ago by tatzka.
Viewing 2 replies - 1 through 2 (of 2 total)
  • MarkRH

    (@markrh)

    Have a look at https://www.php.net/manual/en/function.shell-exec but depending on the configuration of PHP at the host, the shell exec commands might be disabled as it’s a good idea to disable them for security reasons.

    Thread Starter tatzka

    (@tatzka)

    OK. Checked my php.ini, the line
    disable_functions =
    is empty.

    I finally managed to run an shell_exec from the functions.php and execute a test.php script located under /usr/lib/cgi-bin. I also managed to pass parameters to the php script. This is what I did:
    $command=’php /usr/lib/cgi-bin/testcmd.php ‘.$uniquename.’ ‘.$randompasswd;
    $output = shell_exec($command);

    The code for test.php:

    <?php
    mail(‘myemail@gmai.com’,”Subject: test.php”,”shell_exec works and passes forward”\nUniquename: “.$argv[1].’\nRandompasswd: ‘.$argv[2]);
    $result=shell_exec(‘sudo /usr/bin/mosquitto_passwd -b /etc/mosquitto/passwd ‘.$argv[1].’ ‘.$argv[2]);
    ?>

    The executed test.php does run and send out the email with the passed arguments.
    However the mosquitto_passwd tool does not run. I wonder why not?

    Btw. the test.php has the following access rights:

    pi@server:/usr/lib/cgi-bin $ stat -c “%a %A” test.php
    6711 -rws–s–x

    • This reply was modified 6 years ago by tatzka.
    • This reply was modified 6 years ago by tatzka.
Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘How to execute shell commands from within functions.php?’ is closed to new replies.