简体   繁体   English

我该如何拖尾一个远程文件?

[英]How can I tail a remote file?

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. 这是在Linux机器的内部网络上。 The requirements are: 要求是:

  1. Must be well behaved (no extra process laying around, or continuing output) 必须表现良好(不得摆放额外的程序或继续输出)

  2. Cannot require someone's pet Perl module. 不需要别人的宠物Perl模块。

  3. Can be invoked through Perl. 可以通过Perl调用。

  4. If possible, doesn't require a custom built script or utility on the remote machine (regular linux utilities are fine) 如果可能的话,不需要在远程计算机上自定义构建的脚本或实用程序(常规linux实用程序就可以了)

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. 基本尾部不起作用,因为在本地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 $_;
}

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. 这样效果更好,因为在本地进程退出后,屏幕上没有任何输出,但是远程进程无法确定其套接字已关闭并且可以无限期地运行。

Have you tried 你有没有尝试过

ssh -t remotemachine <some command>

-t option from the ssh man page: 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.

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.

您只能尝试Survlog它的OSX。

Some ideas: 一些想法:

  • You could mount it over NFS or CIFS, and then use File::Tail . 您可以通过NFS或CIFS挂载它,然后使用File :: Tail
  • You could use one of Perl's SSH modules (there are a number of them), combined with tail -f . 您可以将Perl的SSH模块之一(有很多)与tail -f结合使用。

You can Tail files remotely using bash and rsync. 您可以使用bash和rsync远程尾部文件。 The following script is taken from this tutorial: Tail files remotely using bash and rsync 以下脚本来自本教程: 使用bash和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

netcat应该为您做。

There is File::Tail . File :: Tail Don't know if it helps? 不知道是否有帮助?

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

Someone suggested using nc (netcat). 有人建议使用nc(netcat)。 This solution does work but is less ideal than just using ssh -t. 此解决方案确实有效,但并不比仅使用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. 最大的问题是,您必须在连接的两边都使用nc,并且需要在本地计算机上进行一些端口发现,以找到合适的端口进行连接。 Here is the adaptation of the above code to use netcat: 这是上面的代码改编为使用netcat的代码:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM