[英]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.