duanmu6752 2012-04-21 06:42 采纳率: 0%
浏览 630
已采纳

SSH连接在重负载下失败

$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条回答 默认 最新

  • doushenyi9104 2012-04-21 07:44
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题