2012-04-21 125 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這可能會幫助診斷問題的日誌。