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

时间:2017-06-27 06:37:33

标签: php windows apache ssh phpseclib

我在windows phpseclib1.0.7上有一个可移植版本的Apache和php7。我想扩展lib以便使用ssh。 使用网站上的股票脚本进行测试

<?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\n");
$output = $ssh->read('#[pP]assword[^:]*:|username@username:~\$#', 
NET_SSH2_READ_REGEX);
echo $output;
if (preg_match('#[pP]assword[^:]*:#', $output)) {
    $ssh->write("password\n");
    echo $ssh->read('username@username:~$');
}
?>

即使缺少信息,也应该抛出错误,但它却给了我

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

这是从第2351行开始的SSH2.php

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;
}
}
}
}

有没有人对如何修复有任何想法?

1 个答案:

答案 0 :(得分:0)

我认为您无法访问www.domain.tld,首先,您必须确保可以从您的系统访问www.domain.tld。 您可以将代码包装在try-catch块中以获取错误

<?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\n");
 $output = $ssh->read('#[pP]assword[^:]*:|username@username:~\$#', 
 NET_SSH2_READ_REGEX);
 echo $output;
 if (preg_match('#[pP]assword[^:]*:#', $output)) {
     $ssh->write("password\n");
     echo $ssh->read('username@username:~$');
 }catch(\Exception $ex){echo "You got an error".$ex->getMessage();}
?>

如果你确定主机可以访问,你可以像这样扩展最大时间:

ini_set('max_execution_time', 300);

如果您确定您的主机是正确的,您可以检查您的主机ssh端口是否已打开并且是否正常工作?