PHP的exec函数

代码如下:

<?php

$out = ['a'=>'apple','b'=>'banana','c'=>'cat','d'=>'dog'];
$shell_return=null;
$v = exec('netstat -tnlp',$out,$shell_return);

print_r($out);
var_dump($shell_return);
var_dump($v);

执行后的输出如下:

Array
(
    [a] => apple
    [b] => banana
    [c] => cat
    [d] => dog
    [0] => Active Internet connections (only servers)
    [1] => Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
    [2] => tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      1/systemd
    [3] => tcp        0      0 192.168.122.1:53        0.0.0.0:*               LISTEN      7311/dnsmasq
    [4] => tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      6987/sshd
    [5] => tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN      6978/cupsd
    [6] => tcp        0      0 127.0.0.1:6011          0.0.0.0:*               LISTEN      16181/sshd: myLinux@pts
    [7] => tcp6       0      0 :::3306                 :::*                    LISTEN      7361/mysqld
    [8] => tcp6       0      0 :::111                  :::*                    LISTEN      1/systemd
    [9] => tcp6       0      0 :::8080                 :::*                    LISTEN      7689/httpd
    [10] => tcp6       0      0 :::80                   :::*                    LISTEN      7689/httpd
    [11] => tcp6       0      0 :::22                   :::*                    LISTEN      6987/sshd
    [12] => tcp6       0      0 ::1:631                 :::*                    LISTEN      6978/cupsd
    [13] => tcp6       0      0 ::1:6011                :::*                    LISTEN      16181/sshd: myLinux@pts
    [14] => tcp6       0      0 :::8000                 :::*                    LISTEN      7689/httpd
)
int(0)
string(90) "tcp6       0      0 :::8000                 :::*                    LISTEN      7689/httpd"

结论:

exec($shell, $output, $shell_return)

  1. $shell:  shell命令, 如: 'netstat -tnlp'
  2. $output:  执行shell命令后的输出结果, 注意: 如果$output之前有值, 在将输出的数据追加到原来的数据之后
  3. $shell_return:  shell命令的执行结果, 如果执行成功,返回0, 失败为其他值
  4. exec函数执行后的返回值: 返回$output数组的最后一条数据
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值