php ssh2_exec 不执行“su”命令

发布于 2024-11-02 06:26:52 字数 2357 浏览 1 评论 0 原文

我对 php 的 ssh2 有很多乐趣。(!)

我正在通过 ssh-ing 进入本地主机(运行 ubuntu)进行测试。我已经成功地使用我的用户名(而不是 root )进行连接和身份验证,并且一些命令(例如“ls”返回一些信息,这是有希望的。肯定会到达某个地方。

接下来我想要做的是发出“su”命令,然后给出 root 密码。

我没有收到错误,并且返回了资源,但流中似乎没有数据(我希望出现“密码:”提示)。不直接使用 root 密码进​​行身份验证,因为那是ssh 被禁用。

你认为

我应该期待“密码:”提示吗?

借用自

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

提前感谢您的帮助:)

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

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.

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

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.

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

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

Here's 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

多彩岁月 2024-11-09 06:26:53

您应该尝试phpseclib - 纯 PHP SSH 实现 的最新 SVN 版本。以下是您如何使用 su 的方法:

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

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]');
?>
夜唯美灬不弃 2024-11-09 06:26:53

也许尝试诸如 bash -c su 或 su root 和 bash -c "su root" 之类的东西?

Maybe try something like bash -c su or su root and bash -c "su root"?

她如夕阳 2024-11-09 06:26:53

您所要做的就是在执行“su”命令时附加您的超级用户密码。您可以对您的代码进行以下修改。

$cmd = "su";
$cmd = "echo '" . $sshPassword . "' | sudo -S " . $cmd;
if (!($stream = ssh2_exec($con, $cmd))) { 
... 
}

All you have to do is append your superuser password at the moment you execute the "su" command. You can make the following modification to your code.

$cmd = "su";
$cmd = "echo '" . $sshPassword . "' | sudo -S " . $cmd;
if (!($stream = ssh2_exec($con, $cmd))) { 
... 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文