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ñ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.
the problems are here:
So the code will result in:
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.