php ssh2_exec 不执行“su"命令 [英] php ssh2_exec not executing 'su' command

查看:28
本文介绍了php ssh2_exec 不执行“su"命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 ssh2 for php 中玩得很开心.(!)

I'm having lots of fun with ssh2 for php.(!)

我正在通过 ssh-ing 测试到 localhost(运行 ubuntu).我已经设法使用我的用户名(不是 root )进行连接和身份验证,以及一些命令(如ls"返回一些信息,这是有希望的.肯定会到达某个地方.

I am testing by ssh-ing into localhost (running ubuntu). I have managed to connect and authenticate with my username( not root ), and some commands (like 'ls' return some info, which is promising. Definitely getting somewhere.

接下来我想要做的是发出su"命令,然后提供 root 密码.

What I want to be able to do next is issue an 'su' command and then give the root password.

我没有收到错误,并且返回了一个资源,但流中似乎没有数据.(我有点期待密码:"提示).我无法直接使用 root 密码进​​行身份验证,因为 ssh 禁用了该密码.

I don't get an error, and a resource is returned, but there seems to be no data in the stream. (I'm kind of expecting a 'Password:' prompt). I can't authenticate directly with the root password, because that is disabled for ssh.

你认为su"有什么理由会返回一些文本?

Is there any reason why 'su' would return with some text, do you think?

我应该期待返回密码:"提示吗?

Should I be expecting the 'Password:' prompt back?

这是我的代码:

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);
                }
            }
        }
    }

借鉴自http://kevin.vanzonneveld.net/techblog/article/make_ssh_connections_with_php/尊重.

我得到的输出是:

okay: logged in...
Resource id #3
after stream_set_blocking
data len: 0

在此先感谢您的帮助:)

Thanks in advance for any help :)

推荐答案

您应该尝试最新的 SVN 版本 phpseclib - a纯 PHP SSH 实现 - 相反.以下是您如何使用它:

You should try the latest SVN version of phpseclib - a pure PHP SSH implementation - instead. Here's how you'd do su with that:

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

$ssh = new Net_SSH2('localhost', 22);
$ssh->login('username', 'password');

$ssh->read('[prompt]');
$ssh->write("su - user\n");
$ssh->read('Password:');
$ssh->write("Password\n");
echo $ssh->read('[prompt]');
?>

这篇关于php ssh2_exec 不执行“su"命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆