如何使PHP列出所有Linux用户? [英] how to make PHP lists all Linux Users?

查看:81
本文介绍了如何使PHP列出所有Linux用户?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想建立一个基于php的网站(自动化)我的Ubuntu服务器上的一些命令

I want to build a php based site that (automate) some commands on my Ubuntu Server

我做的第一件事是去文件(sudoers)用户www-data所以我可以用root权限执行php命令!

first thing I did was going to the file (sudoers) and add the user www-data so I can execute php commands with root privileges!

# running the web apps with root power!!!
www-data    ALL=(ALL) NOPASSWD: ALL

/ p>

then my PHP code was

<?php
   $command = "cat /etc/passwd | cut -d\":\" -f1";
   echo 'running the command: <b>'.$command."</b><br />";
   echo exec($command);
?>

它只返回一个用户(最后一个用户)!如何让所有用户都返回?

it returns only one user (the last user) !!! how to make it return all users?

谢谢

推荐答案

从exec的PHP手册:

From the PHP manual on exec:


返回值

Return Values


命令的结果。如果需要执行
命令,并且直接将来自
命令的所有数据直接返回,而没有任何干扰,则使用passthru()
函数。
要获取已执行命令的输出,请确保设置并使用
输出参数。

The last line from the result of the command. If you need to execute a command and have all the data from the command passed directly back without any interference, use the passthru() function. To get the output of the executed command, be sure to set and use the output parameter.

所以你必须做类似这样的事情:

So you have to do something similar to this:

<?php
   $output = array();
   $command = "cat /etc/passwd | cut -d\":\" -f1";
   echo 'running the command: <b>'.$command."</b><br />";
   exec($command, &$output);
   echo implode("<br />\n", $output);
?>

这篇关于如何使PHP列出所有Linux用户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆