How get data from a document and compare with variable

110 views Asked by At

I'm newbe and I'm trying to do this:

            }else{ //Si se ha pulsado el boton anyadir

            //Recojo datos del formulario
            $usuario = $_POST['nomusuario'];

            $comprobacion = $ssh->exec("awk -F ":" '{print $1}' /etc/passwd | grep $usuario", NET_SSH2_READ_SIMPLE);
            $filtrado = array_filter(explode($comprobacion));

            if($filtrado!=$usuario){
                echo '<div class="alert alert-warning">';
                    echo 'User '.$usuario.' exists.';
                echo '</div';
                echo $filtrado;
            }echo{

but when I run de code always show "User tal exists", and when I add new user too! I don't understand.

How can I compare a get content document with a variable?

I paste all code:

<?php

            set_include_path(get_include_path() . PATH_SEPARATOR . '/var/www/html/phpseclib');

            include ('/var/www/html/phpseclib/Net/SSH2.php');
            include ('/var/www/html/phpseclib/Crypt/RSA.php');

            $ipConexion=$_REQUEST['ipconexion'];
            $ipConexion='10.0.0.107';
            $sudo_password='retaco';

            define('NET_SSH2_LOGGING', NET_SSH2_LOG_COMPLEX);

            $ssh = new Net_SSH2($ipConexion);
            $key = new Crypt_RSA();
            $key->loadKey(file_get_contents('/var/www/html/phpseclib/Crypt/id_rsa'));

            if (!$ssh->login('pi', $key)) {
                exit('Login failed:');
            }

        if(!isset($_POST['anyadir'])){ //Si NO se ha pulsado el boton anyadir

            echo '<form action="users.add.php" method="post">';

            echo '<input type="text" class="form-control" placeholder="usuario" name="nomusuario" />';

            ...

            echo 'El host conectado sporta '; echo $viewShell = $ssh->exec('cat /etc/shells | tail -n+2', NET_SSH2_READ_SIMPLE);
                    echo '<hr />';

            echo '<button type="submit" value="anyadir" name="anyadir" class="btn btn-default">A&ntilde;adir usuario</button>';
            echo '</form>';

        }else{ //Si se ha pulsado el boton anyadir

            //Recojo datos del formulario
            $usuario = $_POST['nomusuario'];

            $comprobacion = $ssh->exec("awk -F ":" '{print $1}' /etc/passwd | grep $usuario", NET_SSH2_READ_SIMPLE);
            $filtrado = array_filter(explode($comprobacion));

            if($filtrado!=$usuario){
                echo '<div class="alert alert-warning">';
                    echo 'El usuario '.$usuario.' ya existe.';
                echo '</div';
                echo $filtrado;
            }else{

                //echo '<div class="alert alert-success">';
                //    $useradd = $ssh->exec('useradd '.$usuario, NET_SSH2_READ_SIMPLE);
                //echo '</div';

                $ssh->read('/.*@.*[$|#]/', NET_SSH2_READ_REGEX);
                $ssh->write("sudo useradd ".$usuario."\n");

                $output = $ssh->read('#[pP]assword[^:]*:|username@username:~\$#', NET_SSH2_READ_REGEX);
                //echo $output;

                    if (preg_match('#[pP]assword[^:]*:|username@username:~\$#', $output)) {
                        $ssh->write($sudo_password."\n");
                        $ssh->read('username@username:~$');
                    }
                echo "Usuario agregado correctamente";
                echo '</pre>';
            }

        }

        ?>

Thanks for your help!! OM.

3

There are 3 answers

0
Lukas On BEST ANSWER

the problems are here:

$comprobacion  = 'awk -...'; // you've got a name from awk
$filtrado = array_filter(explode($comprobacion)); // but explode needs 2 parameters

So the code will result in:

Warning: explode() expects at least 2 parameters, 1 given .... on line ...
Warning: array_filter() expects parameter 1 to be array, null given in .... on line ...

I assume, you have the directive "display_errors" set to Off, so check please your logs, to see these warnings.

The value of variable $filtrado will be set to NULL then, and the $usuario has a value. In result the condition if($filtrado!=$usuario) will always be true, and you got the message.

0
Novatus On

Many thanks for your suggestions. I'm could solve the problem with your help...

new code:

            }else{ //Si se ha pulsado el boton anyadir

            //Recojo datos del formulario
            $usuario = $_POST['nomusuario'];
            $directorio = $_POST['directorio'];
            $grupos = $_POST['grupo'];
            $shell = $_POST['shell'];
            $comentario = $_POST['comentario'];

            $comprobacion = $ssh->exec('cat /etc/passwd | grep '.$usuario.' | cut -f1 -d":"', NET_SSH2_READ_SIMPLE);
            //$comprobacion = $ssh->exec("grep -w ".$usuario, NET_SSH2_READ_SIMPLE);

            if($comprobacion!=""){

                echo '<div class="alert alert-warning">';
                    echo 'El usuario '.$usuario.' ya existe.';
                echo '</div';

            }else{

Now, if I write a exist user the code show "thar user exist". If not exist then create a new user.

Sometimes we need a lot of eyes...

I'm very happy. Many many thanks.

Cheers! OM.

0
Joe C On

array_filter returns an array, while I would presume that $usuario is a string. Those will never be equal.