簡體   English   中英

我該如何拖尾一個遠程文件?

[英]How can I tail a remote file?

我正在嘗試找到一種在遠程主機上尾隨文件的好方法。 這是在Linux機器的內部網絡上。 要求是:

  1. 必須表現良好(不得擺放額外的程序或繼續輸出)

  2. 不需要別人的寵物Perl模塊。

  3. 可以通過Perl調用。

  4. 如果可能的話,不需要在遠程計算機上自定義構建的腳本或實用程序(常規linux實用程序就可以了)

我嘗試過的解決方案通常是這種類型的

ssh remotemachine -f <some command>

“某些命令”為:

tail -f logfile

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

這樣效果更好,因為在本地進程退出后,屏幕上沒有任何輸出,但是遠程進程無法確定其套接字已關閉並且可以無限期地運行。

你有沒有嘗試過

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.

您只能嘗試Survlog它的OSX。

一些想法:

  • 您可以通過NFS或CIFS掛載它,然后使用File :: Tail
  • 您可以將Perl的SSH模塊之一(有很多)與tail -f結合使用。

您可以使用bash和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應該為您做。

File :: Tail 不知道是否有幫助?

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

有人建議使用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");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM