我正在尝试使用 Laravel 内置的 SSH 功能从远程服务器获取 git 日志。一切正常,但通过 SSH 连接、获取日志、将提交转换为数组然后在 View 中显示它们需要大约 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
- 那么你可以试试上面的例子
注意:如果您在这里遇到安装注释问题,我可以帮助您。
关于php - Laravel 远程 ssh 很慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30408026/