你如何在 PHP 中通过 SFTP 获取最后几行文件 [英] How do you get last some lines of file via SFTP in PHP

查看:60
本文介绍了你如何在 PHP 中通过 SFTP 获取最后几行文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要登录到生产服务器来检索一个文件并使用该文件中的数据更新我的数据库.由于这是一个生产数据库,我不想每 5 分钟获取一次整个文件,因为文件可能很大,这可能会影响服务器.我需要每 5 分钟间隔获取此文件的最后 30 行,并且影响尽可能小.

I need to login to a production server retrieve a file and update my data base with the data in this file. Since this is a production database, I don't want to get the whole file every 5 minutes since the file may be huge and this may impact the server. I need to get the last 30 lines of this file every 5 minutes interval and have as little impact as possible.

以下是我当前的代码,如果您对如何最好地完成此操作有任何见解,我将不胜感激:

The following is my current code, I would appreciate any insight to how best accomplish this:

<?php

$user="id";
$pass="passed";
$c = curl_init("sftp://$user:$pass@server1.example.net/opt/vmstat_server1");
curl_setopt($c, CURLOPT_PROTOCOLS, CURLPROTO_SFTP);
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($c);
curl_close($c);
$data = explode("\n", $data);
?>

推荐答案

SFTP 不支持部分文件传输.使用 fullblowin SSH 连接并使用远程tail"操作获取文件的最后几行可能会更好,例如

SFTP is not capable of partial file transfers. You might have better luck using a fullblowin SSH connection and use a remote 'tail' operation to get the last lines of the file, e.g.

$lines = shell_exec("ssh user@remote.host 'tail -30 the_file'");

当然,您可能想要一些更强大的东西来处理诸如阻止 ssh 通过的 net.glitches 之类的事情,但作为一个基本的起点,这应该可以解决问题.

Of course, you might want to have something a little more robust that can handle things like net.glitches that prevent ssh from getting through, but as a basic starting point, this should do the trick.

这篇关于你如何在 PHP 中通过 SFTP 获取最后几行文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆