2012-04-21 88 views
2
$con = ssh2_connect($host, 22); 
ssh2_auth_password($con, $rem_acc, $pass); 
ssh2_scp_send($con,$rand.".gz","./".$rand.".gz"); 
$stream = ssh2_exec($con, "./exeonserv.sh ".$rand); 

下未能只要我保持负载低于2请求每秒的PHP脚本(有脚本2个SSH连接,每秒所以4个连接),这工作正常SSH连接重载

但超过每秒2所请求的那一刻,连接开始出现问题,这种错误日志中:

[周六4月21日11时51分四十秒2012] [错误] [客户172.16.57.97 ] PHP警告:ssh2_connect():启动SSH连接时出错(-1):无法在第105行的/var/www/fsproj/result.php中获取横幅 [Sat Apr 21 11:51:40 2012] [错误] [客户端172.16.57.97] PHP警告:ssh2_connect():无法连接到位于/var/www/fsproj/result.php的本地主机105行

我用下面的代码尝试解决问题,但是如果持续负载大于2req /秒。它只是增加了响应时间

$con=false;  
while(!$con) 
{ 
    $con = ssh2_connect($host, 22); 
} 

SSH连接可以打开的最大速率是否有上限?如果是的话,我可以在哪里更改该值? (或任何其他解决办法?)

我使用Apache在Ubuntu

回答

2

考虑看看man sshd_config,以下各节似乎控制可以在一旦打开SSH连接的最大数目以及最大并发连接尝试次数。您将需要修改/etc/ssh/sshd_config与您所需的设置。

 MaxSessions 
      Specifies the maximum number of open sessions permitted per net- 
      work connection. The default is 10. 

    MaxStartups 
      Specifies the maximum number of concurrent unauthenticated con- 
      nections to the SSH daemon. Additional connections will be 
      dropped until authentication succeeds or the LoginGraceTime 
      expires for a connection. The default is 10. 

      Alternatively, random early drop can be enabled by specifying the 
      three colon separated values ``start:rate:full'' (e.g. 
      "10:30:60"). sshd(8) will refuse connection attempts with a 
      probability of ``rate/100'' (30%) if there are currently 
      ``start'' (10) unauthenticated connections. The probability 
      increases linearly and all connection attempts are refused if the 
      number of unauthenticated connections reaches ``full'' (60). 

此外,对于你的榜样,你试图连接到服务器时,您可能需要连接尝试失败后添加sleep。没有这种退避并且服务器被淹没,您的脚本可能会通过尝试连接尝试淹没服务器而使情况更糟。

1

老实说,我会使用phpseclib, a pure PHP SSH implementation

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

$ssh = new Net_SSH2('www.domain.tld'); 
if (!$ssh->login('username', 'password')) { 
    exit('Login Failed'); 
} 

echo $ssh->exec('pwd'); 
echo $ssh->exec('ls -la'); 
?> 

phpseclib比libssh2更便携,你可以得到phpseclib这可能会帮助诊断问题的日志。