3

I am attemping to use the phpseclib to change the password of my remote server through its NET_SSH2 functions. Below is what I am using.

    <?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]');

    ?>

Everytime my script just hangs and doesn't seem to do much of anything. Anything I could be doing wrong here?

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

5
  • 2
    If you run your program under strace(1), what are the exact strings that it does read? Why don't you wait for the initial shell prompt? If you add debugging echo statements, where exactly does it stall? Commented Apr 12, 2012 at 3:20
  • Turns out I capitalized a single p letter. Works like a charm now. Thanks for your detailed response either way. Commented Apr 12, 2012 at 4:41
  • 2
    @user1328041 if you yourself found and answer, then feel free to delete the question so others won't read it Commented Apr 12, 2012 at 22:51
  • @user1328041: Please add your solution as an answer below and accept it. Commented Apr 14, 2012 at 1:58
  • You need "$ssh->read" before "$ssh->write". I posted code below. Commented Jan 31, 2013 at 20:07

2 Answers 2

2

This may help you

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

You can execute any linux command through shell_exec function

Sign up to request clarification or add additional context in comments.

Comments

1

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.');

?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.