0

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 :)

4
  • ss might not be available as a command in PHP's context; try /usr/sbin/ss ... Commented Aug 12, 2021 at 20:30
  • 1
    Have a look here: PHP quotes inside quotes. Commented Aug 12, 2021 at 20:31
  • Yes KIKO thankyou thats the answer :) I needed to escape my single quotes- ill mark as complete Commented Aug 12, 2021 at 20:38
  • 1
    Ah, no longer confused! 😉 Commented Aug 12, 2021 at 20:49

1 Answer 1

1

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>";
?>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.