如何跟踪远程文件?

发布于 2024-07-14 01:57:23 字数 757 浏览 5 评论 0原文

我正在尝试找到一种在远程主机上跟踪文件的好方法。 这是在 Linux 机器的内部网络上。 要求是:

  1. 必须表现良好(没有额外的进程,或持续输出)

  2. 不能需要某人的宠物 Perl 模块.

  3. 可以通过 Perl 调用。

  4. 如果可能,不需要在远程计算机上自定义构建的脚本或实用程序(常规 Linux 实用程序就可以)

我尝试过的解决方案通常是这种

ssh remotemachine -f <some command>

“某些命令”:

tail -f logfile

基本 tail 不起作用,因为在本地 ssh 进程终止后,远程进程继续将输出写入终端。

$socket = IO:Socket::INET->new(...);
$pid = fork();
if(!$pid)
{
  exec("ssh $host -f '<script which connects to socket and writes>'");
  exit;
}

$client = $socket->accept;
while(<$client>)
{
  print $_;
}

这效果更好,因为本地进程退出后屏幕上没有输出,但远程进程没有发现它的套接字已关闭并且它会无限期地存在。

I am trying to find a good way to tail a file on a remote host. This is on an internal network of Linux machines. The requirements are:

  1. Must be well behaved (no extra process laying around, or continuing output)

  2. Cannot require someone's pet Perl module.

  3. Can be invoked through Perl.

  4. If possible, doesn't require a custom built script or utility on the remote machine (regular linux utilities are fine)

The solutions I have tried are generally of this sort

ssh remotemachine -f <some command>

"some command" has been:

tail -f logfile

Basic tail doesn't work because the remote process continues to write output to the terminal after the local ssh process dies.

$socket = IO:Socket::INET->new(...);
$pid = fork();
if(!$pid)
{
  exec("ssh $host -f '<script which connects to socket and writes>'");
  exit;
}

$client = $socket->accept;
while(<$client>)
{
  print $_;
}

This works better because there is no output to the screen after the local process exits but the remote process doesn't figure out that its socket is down and it lives on indefinitely.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

风流物 2024-07-21 01:57:24

netcat 应该为你做这件事。

netcat should do it for you.

月朦胧 2024-07-21 01:57:24

您可以使用 bash 和 rsync 远程尾随文件。 以下脚本取自本教程: 使用 bash 远程尾部文件同步

#!/bin/bash
#Code Snippet from and copyright by sshadmincontrol.com
#You may use this code freely as long as you keep this notice.

PIDHOME=/a_place/to/store/flag/file
FILE=`echo ${0} | sed 's:.*/::'`
RUNFILEFLAG=${PIDHOME}/${FILE}.running

if [ -e $RUNFILEFLAG ]; then
   echo "Already running ${RUNFILEFLAG}"
   exit 1
else
   touch ${RUNFILEFLAG}
fi

hostname=$1 #host name to remotlely access
log_dir=$2  #log directory on the remotehost
log_file=$3 #remote log file name
username=$3 #username to use to access remote host
log_base=$4 #where to save the log locally

ORIGLOG="$log_base/$hostname/${log_file}.orig"
INTERLOG="$log_base/$hostname/${log_file}.inter"
FINALLOG="$log_base/$hostname/${log_file}.log"

rsync -q -e ssh $username@$hostname:$log_dir/$log_file ${ORIGLOG}
grep -Ev ".ico|.jpg|.gif|.png|.css" > ${INTERLOG}  

if [ ! -e $FINALLOG ]; then
   cp  ${INTERLOG} ${FINALLOG}
else
   LINE=`tail -1 ${FINALLOG}`
   grep -F "$LINE" -A 999999999 ${INTERLOG} \
      | grep -Fv "$LINE" >> ${FINALLOG}
fi

rm ${RUNFILEFLAG}
exit 0

You can Tail files remotely using bash and rsync. The following script is taken from this tutorial: Tail files remotely using bash and rsync

#!/bin/bash
#Code Snippet from and copyright by sshadmincontrol.com
#You may use this code freely as long as you keep this notice.

PIDHOME=/a_place/to/store/flag/file
FILE=`echo ${0} | sed 's:.*/::'`
RUNFILEFLAG=${PIDHOME}/${FILE}.running

if [ -e $RUNFILEFLAG ]; then
   echo "Already running ${RUNFILEFLAG}"
   exit 1
else
   touch ${RUNFILEFLAG}
fi

hostname=$1 #host name to remotlely access
log_dir=$2  #log directory on the remotehost
log_file=$3 #remote log file name
username=$3 #username to use to access remote host
log_base=$4 #where to save the log locally

ORIGLOG="$log_base/$hostname/${log_file}.orig"
INTERLOG="$log_base/$hostname/${log_file}.inter"
FINALLOG="$log_base/$hostname/${log_file}.log"

rsync -q -e ssh $username@$hostname:$log_dir/$log_file ${ORIGLOG}
grep -Ev ".ico|.jpg|.gif|.png|.css" > ${INTERLOG}  

if [ ! -e $FINALLOG ]; then
   cp  ${INTERLOG} ${FINALLOG}
else
   LINE=`tail -1 ${FINALLOG}`
   grep -F "$LINE" -A 999999999 ${INTERLOG} \
      | grep -Fv "$LINE" >> ${FINALLOG}
fi

rm ${RUNFILEFLAG}
exit 0
天邊彩虹 2024-07-21 01:57:24

rsync://[USER@]HOST[:PORT]/SRC...[DEST] | rsync://[USER@]HOST[:PORT]/SRC...[DEST] | 尾巴[目的地]?

rsync://[USER@]HOST[:PORT]/SRC... [DEST] | tail [DEST] ?

柠檬色的秋千 2024-07-21 01:57:24

有人建议使用 nc (netcat)。 该解决方案确实有效,但不如仅使用 ssh -t 理想。 最大的问题是,您必须在连接的两端都使用 nc,并且需要在本地计算机上进行一些端口发现,以找到合适的端口进行连接。 以下是对上述代码的修改以使用 netcat:

$pid = fork();
if(!$pid)
{
  exec("ssh $host -f 'tail -f $filename |nc $localhost $port'");
  exit;
}

exec("nc -l -p $port");

Someone suggested using nc (netcat). This solution does work but is less ideal than just using ssh -t. The biggest problem is that you have to use nc on both sides of the connection and need to do some port discovery on the local machine to find a suitable port over which to connect. Here is the adaptation of the above code to use netcat:

$pid = fork();
if(!$pid)
{
  exec("ssh $host -f 'tail -f $filename |nc $localhost $port'");
  exit;
}

exec("nc -l -p $port");
北方的巷 2024-07-21 01:57:24

File::Tail。 不知道有帮助吗?

There is File::Tail. Don't know if it helps?

淡笑忘祈一世凡恋 2024-07-21 01:57:23

您是否尝试过

ssh -t remotemachine <some command>

ssh 手册页中的 -t 选项:

 -t      Force pseudo-tty allocation. This can be used to execute 
         arbitrary screen-based programs on a remote machine, which
         can be very useful, e.g. when implementing menu services.
         Multiple -t options force tty allocation, even if ssh has no local tty.

而不是

 -f      Requests ssh to go to background just before command execution.  
         This is useful if ssh is going to ask for passwords or passphrases, 
         but the user wants it in the background.
         This implies -n.  The recommended way to start X11 programs at a remote
         site is with something like ssh -f host xterm.

Have you tried

ssh -t remotemachine <some command>

-t option from the ssh man page:

 -t      Force pseudo-tty allocation. This can be used to execute 
         arbitrary screen-based programs on a remote machine, which
         can be very useful, e.g. when implementing menu services.
         Multiple -t options force tty allocation, even if ssh has no local tty.

instead of

 -f      Requests ssh to go to background just before command execution.  
         This is useful if ssh is going to ask for passwords or passphrases, 
         but the user wants it in the background.
         This implies -n.  The recommended way to start X11 programs at a remote
         site is with something like ssh -f host xterm.
感性不性感 2024-07-21 01:57:23

一些想法:

  • 您可以通过 NFS 或 CIFS 挂载它,然后使用 File::Tail。
  • 您可以将 Perl 的 SSH 模块之一(有很多)与 tail -f 结合使用。

Some ideas:

  • You could mount it over NFS or CIFS, and then use File::Tail.
  • You could use one of Perl's SSH modules (there are a number of them), combined with tail -f.
你曾走过我的故事 2024-07-21 01:57:23

您可以尝试 Survlog 仅限 OS X。

You could try Survlog Its OS X only though.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文