I have a lot of fun with ssh2 for php. (!)
I am testing ssh-ing in localhost (running ubuntu). I managed to connect and authenticate with my username (not root), and some commands (for example, "ls" return some information that is promising. Definitely, somewhere.
What I want to do next is issue the command "su" and then specify the root password.
I do not receive an error message and the resource is returned, but there is no data in the stream. (I kind of expect the "Password:" prompt). I cannot authenticate directly with root password because it is disabled for ssh.
Is there a reason why su will come back with some text, what do you think?
Should I expect a Password: request back?
Here is my code:
function changeServerPassword( $ip, $port, $sshUser, $sshPassword, $rootPassword, $newRootPassword, $newSSHPassword = false) { // login to server using $sshUser and $sshPassword // su as root and enter $rootPassword // if any of the above steps fail, return with appropriate error message if (!function_exists("ssh2_connect")) die("function ssh2_connect doesn't exist"); // log in // Do I have to make sure that port is a number? if(!($con = ssh2_connect($ip, $port))){ echo "fail: unable to establish connection\n"; } else { // try to authenticate with username root, password secretpassword if(!ssh2_auth_password($con, $sshUser, $sshPassword)) { echo "fail: unable to authenticate\n"; } else { // alright, we're in! echo "okay: logged in...<br />"; // if (!($stream = ssh2_exec($con, "su"))) { echo "fail: unable to execute command\n"; } else { echo $stream."<br />"; // collect returning data from command stream_set_blocking($stream, true); echo "after stream_set_blocking<br />"; $data = ""; while ($buf = fread($stream,4096)) { $data .= $buf; } echo "data len: " . strlen($data) . "<br />"; echo $data."<br />"; fclose($stream); } } } }
borrowed from http://kevin.vanzonneveld.net/techblog/article/make_ssh_connections_with_php/ respect.
The output I get is:
okay: logged in... Resource id #3 after stream_set_blocking data len: 0
Thanks in advance for any help :)
Joe