【问题标题】:PHP shell_exec returning string with no line breaksPHP shell_exec 返回没有换行符的字符串
【发布时间】:2017-03-28 07:28:49
【问题描述】:

我正在使用 shell_exec 从 PHP 执行一个 shell 脚本,如下所示:

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

intnacstat.sh 结果是:

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

所以每个命令都在单独的行上。

但是当我在 PHP 上回显 $output 时,一切都在一行中, 基本不可读:

"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 period authentication timer reauthenticate server authentication timer inactivity server authentication

谁能告诉我如何回显 $output 以在命令末尾添加线刹车。 或者任何其他可以做到这一点的方式。

【问题讨论】:

  • intnacstat.sh result is: 你的意思是结果还是内容?
  • 如果我从 cli 运行 ./intnacstat.sh 我得到了 root@netdisco:/var/www/html# ./intnacstat.sh 10.97.31.130 show run interface G0/8 Building configuration。 .. 当前配置:767 字节! interface GigabitEthernet0/8 description ISE dot1x Port switchport access vlan 70
  • 如果我在 intnacstat.sh 的结果中执行 cat -E 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 行尾没有换行符。所以可能期望脚本不行。
  • 像你这样的外部软件很难维护,但这不是 shell_exec 的正常行为,请尝试使用 exec 代替并检查

标签: php shell-exec


【解决方案1】:

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

提示:在显示具有异常格式的文本时使用<pre> 标签(包括换行符以及使用print_r($arr) 打印数组时)

【讨论】:

    【解决方案2】:

    我认为结果是正确的,但被浏览器呈现为text/html

    在打印$output之前添加header("Content-type: text/plain");

    【讨论】:

    • 谢谢你,伙计。你教我新东西真是太棒了!!而且我是相当老的程序员了!
    猜你喜欢
    • 2011-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多