Changing the password of a remote Unix server through PHP SSH

I am trying to use phpseclib to change the password of my remote server through its NET_SSH2 function. Below I use.

    <?php
    require_once('Net/SSH2.php');

    $ssh = new Net_SSH2('server1.server.com');
    if (!$ssh->login('user', 'pass')) {
        exit('Login Failed');
    }

    $ssh->write("passwd\n");
    $ssh->read('(current) UNIX Password:');
    $ssh->write("oldpass\n");
    $ssh->read('New password:');
    $ssh->write("newpass\n");
    $ssh->read('Retype new password:');
    $ssh->write("newpass\n");
    echo $ssh->read('[prompt]');

    ?>

Every time my script just freezes and doesn't seem to do anything. Anything I can do wrong here?

Here is the documentation on this: http://phpseclib.sourceforge.net/documentation/net.html

+3
source share
2 answers

It can help you.

<?php
$output = shell_exec('sudo passwd root');
echo "<pre>$output</pre>";
?>

You can execute any linux command using function shell_exec

+2
source

This should work for you.

<?php
    include('Net/SSH2.php');

    $ssh = new Net_SSH2('www.domain.tld');
    if (!$ssh->login('username', 'password')) {
        exit('Login Failed');
    }

    echo $ssh->read('username@username:~$');
    $ssh->write("passwd\n");
    echo $ssh->read('(current) UNIX password:');
    $ssh->write("oldpassword\n");
    echo $ssh->read('New UNIX password:');
    $ssh->write("newpassword\n");
    echo $ssh->read('Retype new UNIX password:');
    $ssh->write("newpassword\n");
    echo $ssh->read('passwd: all authentication tokens updated successfully.');

?>
+1
source

All Articles