代码如下:
<?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)
- $shell: shell命令, 如: 'netstat -tnlp'
- $output: 执行shell命令后的输出结果, 注意: 如果$output之前有值, 在将输出的数据追加到原来的数据之后
- $shell_return: shell命令的执行结果, 如果执行成功,返回0, 失败为其他值
- exec函数执行后的返回值: 返回$output数组的最后一条数据