繁体   English   中英

Laravel远程SSH非常慢

[英]Laravel remote ssh very slow

我正在尝试使用内置SSH功能的Laravel从远程服务器获取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,您可以从此处下载tar.gz文件。

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

  • 下载tar.gz文件
  • 将内容提取到文件夹
  • 从终端将cd插入该提取的文件夹
  • 执行此python setup.py install
  • 然后您可以尝试类似上面的示例

注意:如果您在这里遇到安装注释的困扰,我可以为您提供帮助。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM