duanjiao2978 2017-06-27 06:37
浏览 101

PHP SSH2:超过30秒的最大执行时间

I have a portable version of Apache and php7 on windows phpseclib1.0.7 . I wanted to expand the lib in order to utilize ssh. using the stock script from the website for testing

<?php
include('Net/SSH2.php');

$ssh = new Net_SSH2('-Redacted-');
if (!$ssh->login('-Redacted-', '-Redacted-')) {
    exit('Login Failed');
}

echo $ssh->read('username@username:~$');
$ssh->write("sudo ls -la
");
$output = $ssh->read('#[pP]assword[^:]*:|username@username:~\$#', 
NET_SSH2_READ_REGEX);
echo $output;
if (preg_match('#[pP]assword[^:]*:#', $output)) {
    $ssh->write("password
");
    echo $ssh->read('username@username:~$');
}
?>

Even with the lack of information it should throw an error but instead it gives me

PHP Fatal error:  Maximum execution time of 30 seconds exceeded in 
C:\Users\-Redacted-\Desktop\Apache2.2\htdocs\Net\SSH2.php on line 2358

Here is the SSH2.php starting at line 2351

if (isset($this->keyboard_requests_responses)) {
for ($i = 0; $i < $num_prompts; $i++) {
if (strlen($response) < 4) {
return false;
}
extract(unpack('Nlength', $this->_string_shift($response, 4)));
// prompt - ie. "Password: "; must not be empty
Line 2358----  $prompt = $this->_string_shift($response, $length);
//$echo = $this->_string_shift($response) != chr(0);
foreach ($this->keyboard_requests_responses as $key => $value) {
if (substr($prompt, 0, strlen($key)) == $key) {
$responses[] = $value;
break;
}
}
}
}

does anyone have any ideas on how to fix?

  • 写回答

1条回答 默认 最新

  • douzhuan1169 2017-06-27 06:50
    关注

    I think you can't reach www.domain.tld, first of all, you have to be sure that www.domain.tld is reachable from your system. you can wrap your code in a try-catch block to get an error

    <?php
    include('Net/SSH2.php');
    try{
     $ssh = new Net_SSH2('-redacted-');
     if (!$ssh->login('-redacted-', '-redacted-')) {
         exit('Login Failed');
          }
    
     echo $ssh->read('username@username:~$');
     $ssh->write("sudo ls -la
    ");
     $output = $ssh->read('#[pP]assword[^:]*:|username@username:~\$#', 
     NET_SSH2_READ_REGEX);
     echo $output;
     if (preg_match('#[pP]assword[^:]*:#', $output)) {
         $ssh->write("password
    ");
         echo $ssh->read('username@username:~$');
     }catch(\Exception $ex){echo "You got an error".$ex->getMessage();}
    ?>
    

    if your are sure that host is reachable you can expand maximum timout like this :

    ini_set('max_execution_time', 300);
    

    if you are sure that your host is correct you can check that your host ssh port is open and work correclty?

    评论

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序