【问题标题】:using phpseclib with net_ssh how to su to root using $ssh->exec使用 phpseclib 和 net_ssh 如何使用 $ssh->exec su 到 root
【发布时间】:2013-02-20 20:30:50
【问题描述】:

所以我需要执行一个命令,但它只会在我 su 到 root(或 sudo )时运行,但我似乎无法弄清楚如何将命令发送到 su 到 root

(我可以使用 loginuser 登录并执行其他命令)

http://phpseclib.sourceforge.net/ssh/examples.html

我的代码如下

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

$ssh = new Net_SSH2('255.255.255.255',22);
if (!$ssh->login('loginuser', 'fakepassword')) {
    exit('Login Failed');
}

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

我也尝试过使用 exec 命令做大致相同的事情,但没有运气

有什么建议吗?

代码的当前版本(不起作用)

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

    $ssh = new Net_SSH2('255.255.99.74',22);
    if (!$ssh->login('loginuser', 'password')) {
        exit('Login Failed');
    }

    echo $ssh->read('loginuser@intranet:/home/login >');
    $ssh->write("su\n");
    echo $ssh->read('Password:');
    $ssh->write("rootpassword\n");
    echo $ssh->read('intranet:/home/login #');
    $ssh->write("cc get_wireless_status\n");
    echo $ssh->read('[prompt]');
?>

登录的putty文本

login as: loginuser
loginuser@255.255.99.74's password:
Last login: Thu Feb 14 13:57:16 2013 from infong1045.lxa.perfora.net


Sophos UTM
(C) Copyrights by Astaro and by others 2000-2012.
For more copyright information look at /doc/astaro-license.txt
or http://www.astaro.com/doc/astaro-license.txt

NOTE: Any modifications done by root will void your support.
      Please use WebAdmin for any configuration changes.

loginuser@intranet:/home/login > su
Password:
intranet:/home/login #

最新版本的代码响应

Last login: Thu Feb 14 14:00:00 2013 from 10.10.10.194 Sophos UTM (C) Copyrights by Astaro and by others 2000-2012. For more copyright information look at /doc/astaro-license.txt or http://www.astaro.com/doc/astaro-license.txt NOTE: Any modifications done by root will void your support. Please use WebAdmin for any configuration changes. loginuser@intranet:/home/login > Last login: Tue Feb 19 11:09:18 2013 from infong1045.lxa.perfora.net Sophos UTM (C) Copyrights by Astaro and by others 2000-2012. For more copyright information look at /doc/astaro-license.txt or http://www.astaro.com/doc/astaro-license.txt NOTE: Any modifications done by root will void your support. Please use WebAdmin for any configuration changes. loginuser@intranet:/home/login > su Password: intranet:/home/login # Last login: Tue Feb 19 11:09:23 2013 from infong1045.lxa.perfora.net Sophos UTM (C) Copyrights by Astaro and by others 2000-2012. For more copyright information look at /doc/astaro-license.txt or http://www.astaro.com/doc/astaro-license.txt NOTE: Any modifications done by root will void your support. Please use WebAdmin for any configuration changes. loginuser@intranet:/home/login > cc get_wireless_status -bash: /usr/local/bin/confd-client.plx: Permission denied loginuser@intranet:/home/login > 

【问题讨论】:

  • 你有错误吗?还是在等待密码时挂起?
  • 对不起,它挂了一段时间,2-3分钟后会出错,但我没有复制错误,但它是这样那样的超时。
  • 'error out' 你有错误信息吗?
  • 我收到这条消息:网关超时网关没有收到来自上游服务器或应用程序的及时响应。当我检查路由器上的 ssh 日志时,我看到它已连接并以“登录用户”身份登录
  • 嗯。那是一条 HTTP 错误消息。你是从命令行还是从网页执行脚本?

标签: php ssh command-line-interface phpseclib


【解决方案1】:

这应该可行:

<?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("su\n");
    echo $ssh->read('Password:');
    $ssh->write("password\n");
    echo $ssh->read('username@username:~#');
    $ssh->write("cc get_wireless_status\n");
    echo $ssh->read('[prompt]');
?>

【讨论】:

  • 你的答案比我的好——我没有注意到操作是使用 exec 作为最后一个命令。不错的收获!
  • 感谢您的回答,但它似乎在等待服务器上做同样的事情
  • 我需要匹配读取行以匹配实际用户名吗? loginuser@intranet:/home/login > loginuser@intranet:/home/login > su 密码:intranet:/home/login #
  • 是的。它需要与提示完全匹配。见phpseclib.sourceforge.net/ssh/examples.html#password,timeout,
  • 退出 ('whatever');在第一次 write() 之后。如果它在那之后挂起,它将显示。如果它在此之前挂起,则不会。然后逐行向下(或向上)移动出口,直到找到它/是/悬挂的位置。
【解决方案2】:
<?php
    include('Net/SSH2.php');

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

    $ssh->setTimeout(5);
    echo $ssh->read('username@username:~$');
    $ssh->write("su\n");
    echo $ssh->read('Password:');
    $ssh->write("password\n");
    echo $ssh->read('username@username:~#');
    $ssh->write("cc get_wireless_status\n");
    echo $ssh->read('[prompt]');
?>

我修改了您的代码 sn-p 以包含 setTimeout()。因此,如果对 read() 的一次调用失败,则该调用将超时并回显到该点为止的数据。

【讨论】:

  • 屏幕输出粘贴在我上面主要问题的底部(非常感谢,我觉得越来越近了)
【解决方案3】:

你可能需要做 echo $ssh-&gt;write("rootfakepassword\n");

即。注意\n。

当你在 putty 或任何你必须按回车的地方运行命令时。这一事实也需要反映在您通过 phpseclib 发送到服务器的内容中。

【讨论】:

    【解决方案4】:

    苏不是这里的路。相反,使用 sudo,将自己添加到带有 NOPASSWD 标志的 /etc/sudoers 文件中,然后简单地发出 sudo 命令。你可以找到如何做到这一点here

    或者,您可以在 phpseclib 脚本中使用 expect 来生成 root shell(不建议这样做,这是一个非常肮脏的技巧):

    echo $ssh->exec('expect -c \'log_user 0; set timeout -1; spawn /bin/su; expect "Password:"; send "rootpassword\r"; expect "\r\n"; send "/usr/bin/id\r\n"; log_user 1; expect "uid=0"\'');
    

    在我正在尝试的机器上,我得到以下输出:

    /usr/bin/id

    root@machine:/home/user# /usr/bin/id uid=0(root) gid=0(root) 组=0(根)

    再一次,这个方法很脏并且有很多不需要的输出,我猜你可以修剪一下,如果你读了一点expectdocumentation。很抱歉没有为您提供更清洁的解决方案,但恐怕这已经是最好的了。

    【讨论】:

    • 这是在工作路由器上,无论如何我都不想改变操作系统
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-19
    • 2017-05-25
    • 2018-02-10
    • 2012-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多