PHP期望无法正常工作

时间:2013-07-27 04:49:55

标签: php shell expect administration php-extension

我有php脚本,我用它来自动登录和执行远程机器中的命令。这是我的php代码,

<?php
    ini_set("expect.loguser", "Off");
    $stream = fopen("expect://ssh root@10.3.2.0", "r");
    $cases = array (
        array (0 => "*(yes/no)?", 1 => YESNO),
        array (0 => "*d: ", 1 => PASSWORD),
        array (0 => "*$ ", 1 => SHELL, EXP_EXACT)
    );
    switch (expect_expectl ($stream, $cases)) {
        case YESNO:
            fwrite ($stream, "yes\n");
            break;

        case PASSWORD:
            fwrite ($stream, "myrootpassword\n");
            break;

        case SHELL:
            fwrite ($stream, "top -n 1\n");
            break;

        default:
            die ("Error was occurred while connecting to the remote host!\n");
    }
    while ($line = fgets($stream)) {
        print $line;
    }         
    fclose ($stream);
?>

我知道我已经为php安装了expect模块。

enter image description here

我已经在我的Ubuntu机器中安装了expect。

enter image description here

但我不知道为什么这是脚本无效。我跟着http://www.php.net/manual/en/book.expect.phphttp://php.net/manual/en/expect.examples-usage.php创建了我的php脚本文件。请建议我,因为我是PHP的初学者,我需要做什么修改才能在php中工作。

感谢。

1 个答案:

答案 0 :(得分:0)

首先,大多数服务器默认配置为不允许通过ssh进行远程root登录。

其次,为什么不使用SSH密钥文件来允许无密码登录?它比存储密码更安全。

  1. 设置一个帐户(对于此示例,我们称之为“自动化”)具有无密码sudo访问自动化命令(请参阅sudoers上的NOPASSWD标志)
  2. 使用ssh-keygen从远程系统配置对此帐户的无密码访问
  3. 不是使用expect://来运行命令,而是直接运行命令:

    ssh automate@10.1.2.3 sudo theCommand