PHP的shell_exec触摸重定向和adduser [英] php shell_exec touch redirect and adduser

查看:152
本文介绍了PHP的shell_exec触摸重定向和adduser的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图最终使用php的 shell_exec 函数来创建新的Linux用户。然而,即使在调试时,我也遇到了问题。这里是我的代码

 <?PHP 

函数adduser($ username,$ password,$服务器){
try {
// 3调试语句
$ output = shell_exec(pwd);
echo $ output;
shell_exec(touch test.txt);

// 3个调试语句由Christian
echo'< pre>;
print_r(execute('pwd'));
print_r(execute('touch test.txt'));

//执行代码
$ output = shell_exec(ssh root @ $ server \add​​user $ username; echo $ password | passwd $ username --stdin\);

catch(Exception $ e){
echo'could not add user'。$ e-> getMessage();
}
}

$ servers = array('192.168.1.8');
$ b 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)却无法创建th文件test.txt(即使我给出完整路径/ home / user /。 ../test.txt')。

不用说,添加用户的实际代码也不起作用。



编辑我设法修复了一些代码。 touch test.txt 错误是由于权限不足造成的。 Apache使用用户www-data登录,我只是为该用户创建了一个主文件夹,并确保在该主文件夹中触摸一个文件。



另外根据Christian的要求,三个调试语句现在正在引起麻烦。



编辑进一步检查后,以用户www-data的身份登录时以root身份登录。无法打开/ dev / tty:没有这样的设备或地址。我的猜测是,ssh是问通用的你想永久xxx添加到known_hosts,但我无法回应。有没有办法手动添加一个用户到已知的主机列表?

解决方案

请使用我的功能发现这里

运行以下代码并告诉我们它的输出:

$ p $ echo'< pre>';
print_r(execute('pwd'));
print_r(execute('touch test.txt'));

编辑:如果你想让我的脚本更面向OO: / p>

 函数execute_o($ cmd,$ in){
$ out = execute($ cmd,$ in);
if($ out ['return']!= 0)
抛出新异常('Error'。$ out ['return']。':'。$ out ['stderr']。' 。');
}


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?

解决方案

Please use my function found here.

Run the following code and tell us its output:

echo '<pre>';
print_r(execute('pwd'));
print_r(execute('touch test.txt'));

Edit: If you want to make my script more OO oriented:

function execute_o($cmd,$in){
    $out=execute($cmd,$in);
    if($out['return']!=0)
        throw new Exception('Error '.$out['return'].': '.$out['stderr'].'.');
}

这篇关于PHP的shell_exec触摸重定向和adduser的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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