SSH连接在重负载下失败

  $ con = ssh2_connect($ host,22); 
ssh2_auth_password($ con,$ rem_acc,$ 传递);
ssh2_scp_send($ con,$ rand。“。gz”,“。/”。$ rand。“。gz”);
$ stream = ssh2_exec($ con,“。/ exeonserv.sh”。$ rand);
</ code> </ pre>

只要我将负载保持在每秒2个以下的PHP脚本请求(脚本中有2个SSH连接,所以4个连接 每秒),这样可以正常工作</ p>

但是当它超过每秒2个请求时,连接开始失败,日志中出现此错误:</ p>


[Sat Apr 21 11:51:40 2012] [错误] [client 172.16.57.97] PHP警告:ssh2_connect():启动SSH连接时出错(-1):在/ var /中获取横幅失败 www / fsproj / result.php第105行
\ [[Apr Apr 21 11:51:40 2012] [错误] [client 172.16.57.97] PHP警告:ssh2_connect():无法连接到/ var中的localhost /www/fsproj/result.php第105行</ p>
</ blockquote>

我使用以下代码尝试解决问题 是的,但是如果持续负荷大于2req / sec。 它最终会增加响应时间</ p>

  $ con = false;

while(!$ con)
{
$ con = ssh2_connect($ host,22);
}
</ code> </ pre>

是否有最大上限 可以打开SSH连接的速率? 如果是这样我可以在哪里更改该值? (或任何其他解决方案?)</ p>

我在Ubuntu上使用Apache </ p>
</ div>

展开原文

原文

$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);

As long as I keep the load to below 2 requests per second to the PHP script (there are 2 SSH connections in the script, so 4 connections per second), this works fine

But the moment it exceeds 2 requests per second, the connection starts failing, with this error in the log:

[Sat Apr 21 11:51:40 2012] [error] [client 172.16.57.97] PHP Warning: ssh2_connect(): Error starting up SSH connection(-1): Failed getting banner in /var/www/fsproj/result.php on line 105
[Sat Apr 21 11:51:40 2012] [error] [client 172.16.57.97] PHP Warning: ssh2_connect(): Unable to connect to localhost in /var/www/fsproj/result.php on line 105

I used the following code to try and solve the issue, but if the sustained load is greater than 2req/sec. it just ends up increasing the response time

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

Is there a cap on the maximum rate at which SSH connections can be opened? If so where can I change that value? (or any other solutions?)

I'm using Apache on Ubuntu

2个回答



看一下 man sshd_config </ code>,以下几节似乎控制了可以连接的最大SSH连接数 立即打开,也是最大并发连接尝试次数。 您需要使用所需的设置修改 / etc / ssh / sshd_config </ code>。</ p>

  MaxSessions 
指定每个允许的最大打开会话数 net-
工作连接。 默认值为10.

MaxStartups
指定SSH守护程序的最大并发未经身份验证的连接数。 其他连接将被删除,直到身份验证成功或LoginGraceTime
到期为止。 默认值为10.

或者,可以通过指定
三个冒号分隔值``start:rate:full''(例如
“10:30:60”)来启用随机早期丢弃。 如果当前有“未开始”(10)未经身份验证的连接,则sshd(8)将以“rate / 100”(30%)的概率拒绝连接尝试。 如果
未经身份验证的连接数达到“完整”(60),则概率
线性增加并且所有连接尝试都被拒绝。
</ code> </ pre>

此外,对于 您尝试连接到服务器的示例,您可能需要添加 < 连接尝试失败后,代码> sleep </ code> 。 如果没有这种退避并且服务器被淹没,您的脚本可能会通过尝试使用连接尝试更多地使服务器充满来使事情变得更糟。</ p>
</ div>

展开原文

原文

Taking a look at man sshd_config, the following sections seems to control the maximum number of SSH connections that can be opened at once and also the maximum number of concurrent connection attempts. You'll need to modify /etc/ssh/sshd_config with your desired settings.

     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).

Additionally, for your example where you are attempting to connect to the server, you may want to add a sleep after failed connection attempts. Without this backoff and the server is flooded, your script may make things worse by attempting to flood the server more with connection attempts.



老实说,我会使用 phpseclib,纯PHP SSH实现:</ p>

 &lt;?php 
include('Net / SSH2.php');

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

echo $ ssh-&gt; exec('pwd');
echo $ ssh-&gt; exec('ls -la');
?&gt;
</ code> </ pre>

phpseclib 比libssh2更便携,您可以使用phpseclib获取日志,这可能有助于诊断您的问题。</ p>
</ div>

展开原文

原文

Honestly, I'd use 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 is more portable than libssh2 and you can get logs with phpseclib which might assist in diagnosing your problem.

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐