我试图最终使用 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 -v
返回debug1: read_passphrase: can't open /dev/tty: No such device or address
。我的猜测是 ssh 是在询问通用的“您是否愿意将 xxx 永久添加到 known_hosts”,但我无法回应。无论如何手动将用户添加到已知主机列表?