[英]Laravel remote ssh very slow
I am trying to get the git log from remote server with Laravel built in SSH feature. 我正在尝试使用内置SSH功能的Laravel从远程服务器获取git日志。 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. 一切正常,但是通过SSH连接,获取日志,将提交转换为数组,然后在视图中显示,大约需要7秒钟。 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: 我不知道这是否正常,还是我需要另一种方法,也许是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);
}
I generally do it with Paramiko, its easier 我通常用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)
To install Paramiko, you can download the tar.gz file from here . 要安装Paramiko,您可以从此处下载tar.gz文件。
Assuming you are really new to python, how to install this : 假设您真的是python新手,如何安装它:
tar.gz
file 下载tar.gz
文件 cd
into that extracted folder, from your terminal 从终端将cd
插入该提取的文件夹 python setup.py install
执行此python setup.py install
NOTE : if you get stuck with installation comment here, and I can help you. 注意:如果您在这里遇到安装注释的困扰,我可以为您提供帮助。
I ended up building a python API to execute commands in the server rather then connecting via SSH because using SSH library with PHP slows the web site a lot. 我最终构建了一个Python API,以在服务器中执行命令,而不是通过SSH进行连接,因为将SSH库与PHP一起使用会大大降低网站的速度。 With python executing commands and returning messages/errors it is a lot more faster (under 1 second). 使用python执行命令并返回消息/错误,它的速度要快得多(不到1秒)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.