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

时间:2015-03-17 18:16:37

标签: php shell ssh exec phpseclib

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

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

,结果是

  

egrep:无法打开/testpath/test_displaysub.10912.mml

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

$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中并没有等待完全运行,那么它会跳过checkTimeout()getResult ()这就是为什么它无法正常运行我认为像那样。 如果我认为错了请告诉我。

请告诉我,如果我想使用命令exec(),我应该怎么处理这个问题。

请注意! 我真的没有使用超时,因为我的项目可以输入文件,我不知道该过程想要做多少时间 在我的文件中可以

0988888888
0988888887
0988888886

和我的scrript ./display_test.sh 1 -f filename \n

1 个答案:

答案 0 :(得分:0)

您不必使用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()