Laravel remote ssh very slow

TooCooL

I am trying to get the git log from remote server with Laravel built in SSH feature. Everything works but it takes ~7 seconds to connect over SSH, get the log, convert commits into array and then display them in the view. I don't know if it is normal or I need another approach for this, maybe python, cgi ... This is the function that I have so far:

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);
}
sudhishkr

I generally do it with Paramiko, its easier

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)

To install Paramiko, you can download the tar.gz file from here.

Assuming you are really new to python, how to install this :

  • Download the tar.gz file
  • Extract the contents to a folder
  • cd into that extracted folder, from your terminal
  • execute this python setup.py install
  • then you can try something like the above example

NOTE : if you get stuck with installation comment here, and I can help you.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related