Centos - executing a bash script from PHP

General Tech Bugs & Fixes 2 years ago

0 2 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Posted on 16 Aug 2022, this text provides information on Bugs & Fixes related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (2)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago

 

I have 3 href="https://forum.tuteehub.com/tag/scripts">scripts (I have removed the help_page function from the networkstats.sh script when I pasted here to save some space):

api3.href="https://forum.tuteehub.com/tag/php">php

href="https://forum.tuteehub.com/tag/php">php

output = shell_exec('/bin/bash /usr/share/href="https://forum.tuteehub.com/tag/nginx">nginx/status/getnetworkstatsin.sh');
echo $output;

?>

getnetworkstatsin.sh

#!/bin/bash
ssh -i /tmp/id_rsahref="https://forum.tuteehub.com/tag/1">1 root@centos7clone bash -s -- -I < ./networkstats.sh

networkstats.sh

#!/bin/bash

interface=enp0s3

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

sleep href="https://forum.tuteehub.com/tag/1">1

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

kilobitsin=$(( ( ( inbytesnew - inbytesold ) * 8 ) / href="https://forum.tuteehub.com/tag/1">1024 ))

kilobitsout=$(( ( ( outbytesnew - outbytesold ) * 8 ) / href="https://forum.tuteehub.com/tag/1">1024 ))

show_outgoing() {
echo $kilobitsout
}

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

if [[ $# -eq 0 ]];
then
        help_page
        exit href="https://forum.tuteehub.com/tag/1">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.href="https://forum.tuteehub.com/tag/php">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.href="https://forum.tuteehub.com/tag/php">php. Can someone help, what is the reason behind this? I have added

href="https://forum.tuteehub.com/tag/nginx">nginx ALL=NOPASSWD: /usr/share/href="https://forum.tuteehub.com/tag/nginx">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 href="https://forum.tuteehub.com/tag/scripts">scripts are located inside /usr/share/href="https://forum.tuteehub.com/tag/nginx">nginx/status which href="https://forum.tuteehub.com/tag/nginx">nginx has access to.

profilepic.png
manpreet 2 years ago

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


0 views   0 shares

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.