【发布时间】:2013-07-27 04:49:54
【问题描述】:
我有一个 php 脚本,我在其中使用 expect 来自动登录并在远程机器上执行命令。这是我的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 模块。
我也在我的 Ubuntu 机器上安装了 expect。
但我不知道为什么这是脚本不起作用。我跟着http://www.php.net/manual/en/book.expect.php 和http://php.net/manual/en/expect.examples-usage.php 创建了我的php 脚本文件。请建议我,因为我是PHP的初学者,我需要做哪些修改才能在php中工作。
谢谢。
【问题讨论】:
标签: php shell expect administration php-extension