Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have 3 scripts (I have removed the help_page function from the networkstats.sh script when I pasted here to save some space):

api3.php

<?php

output = shell_exec('/bin/bash /usr/share/nginx/status/getnetworkstatsin.sh');
echo $output;

?>

getnetworkstatsin.sh

#!/bin/bash
ssh -i /tmp/id_rsa1 root@centos7clone bash -s -- -I < ./networkstats.sh

networkstats.sh

#!/bin/bash

interface=enp0s3

read -r inbytesold outbytesold < <(awk -v dev="^$interface:" '$1 ~ dev {
              sub(/[^:]*:/,""); print $1, $9; exit }' /proc/net/dev)

sleep 1

read -r inbytesnew outbytesnew < <(awk -v dev="^$interface:" '$1 ~ dev {
              sub(/[^:]*:/,""); print $1, $9; exit }' /proc/net/dev)

kilobitsin=$(( ( ( inbytesnew - inbytesold ) * 8 ) / 1024 ))

kilobitsout=$(( ( ( outbytesnew - outbytesold ) * 8 ) / 1024 ))

show_outgoing() {
echo $kilobitsout
}

show_all() {
echo "kilobits in: $kilobitsin"
echo "kilobits out: $kilobitsout"
}

if [[ $# -eq 0 ]];
then
        help_page
        exit 1
fi

for arg in "$@"
 do
        case $arg in
                -h|--help)
                help_page
                ;;

                -I)
                show_incoming
                ;;

                -O)
                show_outgoing
                ;;

                -A|--all)
                show_all
                ;;
        esac
 done

The problem I have is that when I execute the api3.php script from console, it is able to execute and return a value.

However when I try and execute from a webpage it fails to return anything.

I believe it is not even executing when I load it via the webpage by navigating to localhost/api3.php. Can someone help, what is the reason behind this? I have added

nginx ALL=NOPASSWD: /usr/share/nginx/status/getnetworkstatsin.sh

To my visudo section, I have tried to change permissions of all files involved to 777 (temporally) without success.

EDIT: I should also mention that all these scripts are located inside /usr/share/nginx/status which nginx has access to.

share|improve this question
    
is output = shell_exec(...) a typo? It should be $output = ..., otherwise you're trying to assign a value to a constant. –  Marc B 21 hours ago
3  
Plus, if you're running this via a webserver, then the webserver ID is EXTREMELY unlikely to have access to the /root dir to get at the root user's key chain, and allowing remote root login via ssh is a major security risk in any case. –  Marc B 21 hours ago
    
hi @MarcB all the scripts are for testing purposes and all locked down via firewall. The nginx server is not public –  user3227965 21 hours ago
    
well, I forgot to move the id_rsa1 file, I thought I had. I will try after moving it and report back –  user3227965 21 hours ago
    
Still no resolve after I moved the id_rsa1 file out of root and managed to execute the script as nginx and login –  user3227965 21 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.