6

所以我需要执行一个命令,但它只会在我 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]');
?>

登录的腻子文本

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 > 
4

4 回答 4

5

这应该有效:

<?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]');
?>
于 2013-02-13T03:19:15.063 回答
1
<?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]');
?>

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

于 2013-02-15T03:06:17.893 回答
0

你可能需要做回声$ssh->write("rootfakepassword\n");

IE。注意\n。

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

于 2013-02-12T19:38:55.350 回答
0

苏不是这里的路。相反,使用sudo ,同时使用NOPASSWD标志将自己添加到/etc/sudoers文件中,然后简单地发出 sudo 命令。您可以在此处了解如何执行此操作。

或者,您可以在 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) groups=0(root)

同样,这种方法很脏,并且有很多不需要的输出,如果您阅读一些期望 文档,我猜您可以对其进行修剪。很抱歉没有为您提供更清洁的解决方案,但恐怕这已经是最好的了。

于 2013-02-20T12:30:50.350 回答