1

I am executing a shell script from PHP with shell_exec like this:

$output = shell_exec('./intnacstat.sh "'.$ip.'" 2>&1');

intnacstat.sh result is:

interface GigabitEthernet0/8
 description ISE dot1x Port
 switchport access vlan 70
 switchport trunk encapsulation dot1q
 switchport trunk native vlan 70
 switchport trunk allowed vlan 256
 switchport mode trunk
 authentication event fail action next-method
 authentication host-mode multi-host
 authentication open
 authentication order dot1x mab
 authentication priority dot1x mab
 authentication port-control auto
 authentication periodic
 authentication timer reauthenticate server
 authentication timer inactivity server
 authentication violation restrict
 mab

So every command is on separate line.

But when I echo $output on PHP everything is on a single line, basically unreadable:

"show run interface G0/8 Building configuration... Current configuration : 767 bytes ! interface GigabitEthernet0/8 description ISE dot1x Port switchport access vlan 70 switchport trunk encapsulation dot1q switchport trunk native vlan 70 switchport trunk allowed vlan 256 switchport mode trunk authentication event fail action next-method authentication host-mode multi-host authentication open authentication order dot1x mab authentication priority dot1x mab authentication port-control auto authentication periodic authentication timer reauthenticate server authentication timer inactivity server authentication

Can someone tell me how to echo the $output to also add line brakes at the end of the command. Or any other way this can be done.

4
  • intnacstat.sh result is: you mean results or contents ? Commented Mar 28, 2017 at 7:49
  • well if i run ./intnacstat.sh from the cli I got root@netdisco:/var/www/html# ./intnacstat.sh 10.97.31.130 show run interface G0/8 Building configuration... Current configuration : 767 bytes ! interface GigabitEthernet0/8 description ISE dot1x Port switchport access vlan 70 Commented Mar 28, 2017 at 8:02
  • if i do cat -E in the resutl of the intnacstat.sh root@netdisco:/var/www/html# cat -E stat.txt $how run interface G0/8 $uilding configuration... $ $urrent configuration : 767 bytes $ $nterface GigabitEthernet0/8 $description ISE dot1x Port $switchport access vlan 70 $switchport trunk encapsulation dot1q there are no line breaks on the end of the lines. So probably the expect script in not ok. Commented Mar 28, 2017 at 8:09
  • it's hard to maintain external software like your case , but it is not the normal behavior for shell_exec , try to use exec instead and check Commented Mar 28, 2017 at 8:31

2 Answers 2

3

I think the result is correct but rendered as text/html by the browser.

Add header("Content-type: text/plain"); before printing $output.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you mate. You are so wonderful to teach me something new!! And I am quite old programmer !
shell_exec is for executing CLI (command line console), not browser
Yes, it's executing on the server, but sending back the output. The browser thinks it's HTML and does not render line breaks. If you change the content type (using header) it will render as text as intended.
2

Try echo "<pre>". $output."</pre>";

Tip: Use the <pre> tags when displaying text with unusual formatting (includes line breaks and when printing arrays using print_r($arr))

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.