exec() PHP 无法等待响应 (phpseclib)

exec() PHP cannot wait the response (phpseclib)

我只是尝试使用 exec() 命令,但它不起作用(我使用 phpseclib)。 这是我的代码

echo $ssh->exec("cd testpath; ./display_test.sh 1 -s 098888888");

结果是

egrep: can't open /testpath/test_displaysub.10912.mml

但是当我 运行 使用 read(), write() 使用此代码时

$ssh->setTimeout(5);
$ssh->read();
$ssh->write("cd testpath \n");
$ssh->read();
$ssh->write("./display_test.sh 1 -s 098888888 \n");
echo $ssh->read();

有效。我想使用命令 exec() 因为它不使用超时。 我认为 exec() 命令是错误的,可能是它不等待响应或跳过该文件中的某些命令 (./display_test.sh) 因为我的代码有

sendCommand (){
    cat $templateLogout >> ${FILE}
    $scriptfile < ${FILE} 1> $processfile 2> tmp #led $processfile to run other
}

checkTimeout(){
    timeout=$(grep "Timed out" tmp|wc -l)
    if [ $timeout -gt 0 ]
    then
        Result="Connection Lost."
        index_timeout=1
    fi
    rm tmp
}

getResult (){
    Result= egrep "response" $mmlfile .........(have more)

当我使用 exec() 时,它会创建 $processfile 但在 $processfile 中不会等待完成 运行ning 然后它会跳到执行 checkTimeout()getResult ()它不能egrep的原因我是这么认为的。 如果我觉得不对请告诉我。

如果我想使用命令 exec().

,请告诉我应该如何处理这个问题

注意! 我真的不使用超时,因为我的项目可以输入文件而且我不知道该进程要执行多少次 在我的档案中可以

0988888888
0988888887
0988888886

我的剧本也./display_test.sh 1 -f filename \n

您不必对 read() / write() 使用超时。试试这个:

$ssh->read('[prompt]');
$ssh->write("cd testpath \n");
$ssh->read('[prompt]');
$ssh->write("./display_test.sh 1 -s 098888888 \n");
echo $ssh->read('[prompt]');

[prompt] 是占位符 - 使用您的实际提示。

W.r.t。 $ssh->exec...也许可以尝试 $ssh->enablePTY(); $ssh->exec(); echo $ssh->read().