Laravel 远程 ssh 很慢

Laravel remote ssh very slow

我正在尝试使用 Laravel 内置 SSH 功能从远程服务器获取 git 日志。一切正常,但通过 SSH 连接、获取日志、将提交转换为数组然后在视图中显示它们需要大约 7 秒。我不知道这是正常的还是我需要另一种方法,也许 python、cgi ... 这是我目前拥有的功能:

public function commits(){
    $commands = array(
        'cd /var/www/web1/public_html',
        'git log -8',
    );

    SSH::into('production')->run($commands, function($line)
    {
        $this->output .= $line.PHP_EOL;
    });
    $arr = explode("\n",$this->output);

    foreach ($arr as $line){
            // Clean Line
            $line = trim($line);
            // Proceed If There Are Any Lines
            if (!empty($line))
            {
                    // Commit
                    if (strpos($line, 'commit') !== false)
                    {
                            $hash = explode(' ', $line);
                            $hash = trim(end($hash));
                            $git_history[$hash] = [
                                    'message' => ''
                            ];
                            $last_hash = $hash;
                            $git_history[$last_hash]['hash'] = $last_hash;
                    }
                    // Author
                    else if (strpos($line, 'Author') !== false) {
                            $author = explode(':', $line);
                            $author = trim(end($author));
                            //email starts with < and ends with >
                            $pattern = sprintf(
                                    '/%s(.+?)%s/ims',
                                    preg_quote('<', '/'), preg_quote('>', '/')
                            );
                            //check pattern and assign the email of the author
                            if (preg_match($pattern, $author, $matches)) {
                                    list(, $match) = $matches;
                                    //echo $match;
                                    $git_history[$last_hash]['author'] = $match;
                                    $user = Sentry::findUserByLogin($git_history[$last_hash]['author']);
                                    $git_history[$last_hash]['avatar'] = $user->avatar;
                            }

                    }
                    // Date
                    else if (strpos($line, 'Date') !== false) {
                            $date = explode(':', $line, 2);
                            $date = trim(end($date));
                            $git_history[$last_hash]['date'] = date('d/m/Y H:i:s A', strtotime($date));
                    }
                    // Message
                    else {
                            $git_history[$last_hash]['message'] .= $line ." ";
                    }
            }
    }
    //$data['server_answer'] = $git_history;
    return Response::json($git_history);
}

我一般用Paramiko做,比较简单

import paramiko

# ssh 
print 'enter ssh'
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) # this will automatically add the keys
ssh.connect(machineHostName, username=user, password=password)

# Run your commands
# example 1 : ls command
print 'do a ls command'
stdin, stdout, stderr = ssh.exec_command('ls')
print stdout.readlines()
time.sleep(2)
# example 2 : change ip address
print 'changing ip address'
stdin, stdout, stderr = ssh.exec_command('sed -i -- s/'+oldIp+'/'+newIp+'/g /etc/sysconfig/network-scripts/ifcfg-eth0')
print stdout.readlines()
time.sleep(2)

要安装 Paramiko,您可以从 here 下载 tar.gz 文件。

假设您是 python 的新手,如何安装:

  • 下载 tar.gz 文件
  • 将内容提取到文件夹
  • cd 从您的终端
  • 进入解压的文件夹
  • 执行这个python setup.py install
  • 那么你可以试试上面的例子

注意:如果您在这里遇到安装问题,我可以帮助您。

我最终构建了一个 python API 来在服务器中执行命令,而不是通过 SSH 连接,因为将 SSH 库与 PHP 一起使用会大大降低网站速度。使用 python 执行命令并返回 messages/errors 它要快得多(不到 1 秒)。