php shell_exec touch 重定向和 adduser
我试图最终使用 php 的 shell_exec 函数来创建新的 Linux 用户。然而,即使进行调试,我也遇到了问题。这是我的代码
<?PHP
function adduser($username,$password,$server){
try{
//3 debug statements
$output=shell_exec("pwd");
echo $output;
shell_exec("touch test.txt");
//3 debug statements are requested by Christian
echo '<pre>';
print_r(execute('pwd'));
print_r(execute('touch test.txt'));
//actuall code
$output=shell_exec("ssh root@$server \"adduser $username; echo $password | passwd $username --stdin\"");
}
catch(Exception $e){
echo 'could not add user '.$e->getMessage();
}
}
$servers = array('192.168.1.8');
foreach($servers as $server){
echo $_GET['USER']." ".$_GET['PASSWORD'];
adduser($_GET['USER'],$_GET['PASSWORD'],$server);
}
try-catch
语句不执行任何操作,使我相信 shell 错误不会作为 PHP 错误传播(Python 也是如此)。 $output=shell_exec("pwd")
行返回正确的目录。然而,shell_exec("touch test.txt")
行无法创建文件 test.txt (即使我给出完整路径 '/home/user/.../test.txt ')。
不用说,添加用户的实际代码也不起作用。
编辑我设法修复了一些代码。 touch test.txt
错误是由于权限不足造成的。 Apache 使用用户 www-data 登录,我只是为该用户创建了一个主文件夹,并确保触摸该主文件夹中的文件。
然而,按照 Christian 的要求添加三个调试语句现在引起了问题。
编辑 经过进一步检查,这与以用户 www-data 登录时无法以 root 身份 ssh 有关。 ssh -v
返回 debug1: read_passphrase: 无法打开 /dev/tty: No such device or address
。我的猜测是 ssh 正在询问通用的“您想将 xxx 永久添加到known_hosts吗”,但我无法回应。是否可以手动将用户添加到已知主机列表中?
I am trying to ultimately use php's shell_exec
function to create new Linux users. I am, however, running into problems even with the debugging. Here is my code
<?PHP
function adduser($username,$password,$server){
try{
//3 debug statements
$output=shell_exec("pwd");
echo $output;
shell_exec("touch test.txt");
//3 debug statements are requested by Christian
echo '<pre>';
print_r(execute('pwd'));
print_r(execute('touch test.txt'));
//actuall code
$output=shell_exec("ssh root@$server \"adduser $username; echo $password | passwd $username --stdin\"");
}
catch(Exception $e){
echo 'could not add user '.$e->getMessage();
}
}
$servers = array('192.168.1.8');
foreach($servers as $server){
echo $_GET['USER']." ".$_GET['PASSWORD'];
adduser($_GET['USER'],$_GET['PASSWORD'],$server);
}
The try-catch
statements don't do anything, leading me to believe that shell errors are not propagated as PHP errors (Python is this way also). The line $output=shell_exec("pwd")
returns the correct directory. The line shell_exec("touch test.txt")
, however, fails to create th file test.txt (even if I give the full path '/home/user/.../test.txt').
Needless to say, the actual code for adding users does not work also.
EDIT I managed to fix some of the code. The touch test.txt
error was as a result of insufficient permissions. Apache logs in with user www-data, I simply created a home folder for that user, and made sure to touch a file in that home folder.
The addition of the three debug statements, as per Christian's requests, are however causing problems now.
EDIT Upon further inspection, it has to do with the inability to ssh as root when logging in as user www-data. ssh -v
returns debug1: read_passphrase: can't open /dev/tty: No such device or address
. My guess is that ssh is asking that generic "would you like to permanently add xxx to known_hosts" but I can't respond. Is there anyway to manually add a user to the known hosts list?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
shell_exec()
抛出异常,一般情况下,我不会让 PHP 顺序执行多个命令,而是编写一个 shell 脚本来执行我需要的所有操作,然后从 PHP 调用它。调试时链条中少了一个环节,我发现我轻松了很多。
关于 SSH 命令本身,由于 apache 是以
www-data
身份执行的,那么它如何以 root 身份登录到有问题的机器呢?您是否已将 apache 用户的密钥添加到远程计算机。shell_exec()
As a general case, rather than having PHP execute several commands sequentially, I write a shell script that does everything I need, then call it from PHP. There's one fewer link in the chain when debugging, and I find I a lot easier.
With regards to your SSH command itself, since apache is executing as
www-data
, how is it logging into the machine in question as root? Have you added the apache user's key to the remote machine.请使用我的函数 在这里找到。
运行以下代码并告诉我们其输出:
编辑: 如果您想让我的脚本更加面向 OO:
Please use my function found here.
Run the following code and tell us its output:
Edit: If you want to make my script more OO oriented: