0

For a current project i need when a variable changes for it then to execute a couple of commands. The current code i have is: (code is in php)

function get_num_clients()
{
    $wlan0_clients = exec('iw dev wlan0 station dump | grep Station | wc -l');    
    if(isset($wlan0_clients)){
        if ($wlan0_clients == "0") {
            return $wlan0_clients;
        } else{
            exec('arp -a > /pinapple/email_log.txt');
            exec('/tmp/emailbash.sh');
            return $wlan0_clients;
        }        
    }
}

As far as i can see it should do it. The purpose of this code is for the result to be sent to a status bar on a php index page which it does but when it doesnt == 0 the rest of the statement isnt processed!

any help would be great. Thanks, Mark

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • 1
    Did you try `var_dump($wlan0_clients);`? – Barmar Feb 11 '19 at 21:49
  • I have managed to get it to notice the change and run the bash script. the next issue is the .txt file is getting written too because i can see the 'last edited' change but for some reason isnt actually putting any data in there. Would you have any idea why? the command works when just running it through shell – Mark Wilkinson Feb 11 '19 at 22:55
  • Try using the full path to the `arp` command, it might not be in the webserver's `$PATH`. – Barmar Feb 11 '19 at 22:57

2 Answers2

0

wc -l indents its output with spaces, you need to remove them.

function get_num_clients()
{
    $wlan0_clients = exec('iw dev wlan0 station dump | grep Station | wc -l');    
    if(isset($wlan0_clients)){
        $wlan0_clients = trim($wlan0_clients);
        if ($wlan0_clients == "0") {
            return $wlan0_clients;
        } else{
            exec('arp -a > /pinapple/email_log.txt');
            exec('/tmp/emailbash.sh');
            return $wlan0_clients;
        }        
    }
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
0

Try this. To find which specific line is giving you issues, insert an 'echo' statement after each line of code, then when you run your code, monitor your console output to see which lines echoed and which ones didn't.

function get_num_clients()
{
   $wlan0_clients = exec('iw dev wlan0 station dump | grep Station | wc -l');
   echo "point 1";    
   if(isset($wlan0_clients)){
     echo "point 2";        
     if ($wlan0_clients == "0") {
       echo "point 3";            
       return $wlan0_clients;
     }else{
       echo "point 4";
       exec('arp -a > /pinapple/email_log.txt');
       echo "point 5";
       exec('/tmp/emailbash.sh');
       echo "point 6";
       return $wlan0_clients;
    }        
  }
}
fazrinwiraman
  • 79
  • 1
  • 11
Russ J
  • 828
  • 5
  • 12
  • 24
  • That was very helpful! i had to fiddle about a bit because the code is getting run on a device that has no console to monitor the output i just have to copy it over and see what happens but the .sh script is running. The only thing ive noticed is the 'arp' command is writing to the file but not putting any of the data in there i dont suppose you would have any idea why? – Mark Wilkinson Feb 11 '19 at 22:46
  • Great. If the answer was helpful, please accept and upvote it. – Russ J Feb 11 '19 at 22:47
  • How does this solve the problem? It's just a debugging technique, it doesn't explain what's actually wrong. – Barmar Feb 11 '19 at 22:58
  • Maybe try 'cat' or 'echo' https://stackoverflow.com/questions/17115664/can-linux-cat-command-be-used-for-writing-text-to-file – Russ J Feb 11 '19 at 23:34