1

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.

All these scripts are located inside /usr/share/nginx/status which nginx has access to.

3
  • networkstats.sh might not get a PATH over web requests and thus does not know where to find the binaries?
    – Karel
    Aug 8, 2014 at 11:24
  • Sounds like you need to either capture STDERR, or look at wherever STDERR gets dumped to (logs).
    – phemmer
    Aug 21, 2014 at 1:04
  • Any PHP errors? You're not properly defining the variable "output" in api3.php.
    – redburn
    Aug 28, 2014 at 15:38

1 Answer 1

2

You don't have an active console when invoking the script from php. Add the -tt switch to your ssh command.

-t Force pseudo-tty allocation. This can be used to execute arbitrary screen-based programs on a remote machine, which can be very useful, e.g. when implementing menu services. Multiple -t options force tty allocation, even if ssh has no local tty.

Source: man ssh

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .