Skip to content
Advertisement

PHP not executing a shell exec command

To keep this simple this code works:

<?php
$sinfo = shell_exec('ss -t -r state established');
echo "<pre>$sinfo</pre>";
?>

And this code code doesn’t:

<?php
$sinfo = shell_exec('ss -H -t -r state established | awk '{$1=$2=""; print $0}'');
echo "<pre>$sinfo</pre>";
?>

Does anyone know how I can get the code that doesn’t work to work?

I have ran the command in a normal terminal and they execute as expected, but when ran with PHP it cause a server 500 error – this seems to require a PHP code review from someone more competent than myself.

Thanks in advance 🙂

Advertisement

Answer

Thanks to KIKO Software his link PHP quotes inside quotes. had the answer that I needed:

I had to escape the single quotes in the AWK

Working code:

<?php
$sinfo = shell_exec('ss -H -t -r state established | awk '{$1=$2=""; print $0}'');
echo "<pre>$sinfo</pre>";
?>
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement