Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Perl 如何跟踪远程文件?_Perl_Scripting_Ssh_Tail_Netcat - Fatal编程技术网

Perl 如何跟踪远程文件?

Perl 如何跟踪远程文件?,perl,scripting,ssh,tail,netcat,Perl,Scripting,Ssh,Tail,Netcat,我正试图找到一种在远程主机上跟踪文件的好方法。这是在Linux机器的内部网络上。这些要求是: 必须表现良好(没有额外的进程或持续输出) 不能需要某人的pet Perl模块 可以通过Perl调用 如果可能,不需要在远程计算机上自定义构建脚本或实用程序(常规linux实用程序也可以) 我尝试过的解决方案通常都是这样的 ssh remotemachine -f <some command> 基本tail不起作用,因为在本地ssh进程死亡后,远程进程继续向终端写入输出 $socket =

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

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

  • 不能需要某人的pet Perl模块

  • 可以通过Perl调用

  • 如果可能,不需要在远程计算机上自定义构建脚本或实用程序(常规linux实用程序也可以)

  • 我尝试过的解决方案通常都是这样的

    ssh remotemachine -f <some command>
    
    基本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 $_;
    }
    
    $socket=IO:socket::INET->new(…);
    $pid=fork();
    如果(!$pid)
    {
    exec(“ssh$host-f'”);
    出口
    }
    $client=$socket->accept;
    while()
    {
    打印美元;
    }
    
    这样做效果更好,因为在本地进程退出后,屏幕上没有输出,但远程进程没有发现其套接字已关闭,并且无限期地继续存在。

    确实存在。不知道它是否有用?

    一些想法:

    • 您可以通过NFS或CIFS装载它,然后使用
    • 您可以使用Perl的一个SSH模块(有很多),与
      tail-f
      结合使用

    rsync://[USER@]HOST[:PORT]/SRC。。。[目的地]|尾部[目的地]

    netcat应该为您做这件事。

    您试过了吗

    ssh -t remotemachine <some command>
    
    而不是

     -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.
    

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

    不过,您只能尝试它的OS X。

    您可以使用bash和rsync远程跟踪文件。以下脚本取自本教程:


    您发布的代码示例毫无意义。你能发布真实的东西吗?你所说的“远程进程继续喷出”是什么意思?当ssh连接的任何一端断开时,另一端也应该断开。。。谜题啊——我看到ssh会话死掉了,通过它们运行的任何东西也会发出嘎嘎声,除非它们是在分离的屏幕会话中运行的something@Aaron:从shell尝试:ssh主机-f“tail-f”然后按Ctrl-C键。在我的RedHat机器上,我继续在终端中获取文件的尾部,远程SSH+尾部仍然非常活跃。使用-t而不是-f解决了这一问题。这本身并不做远程文件。我有一个基于netcat的解决方案,在远程机器上分叉并运行netcat,在本地机器上执行nc侦听器,这很好,但Manni的ssh-t解决方案更好。日志文件的数量和大小使它们与本地机器同步不切实际。rsync可能非常有效,因为它只传输增量。此外,由于它可以压缩数据,而且日志文件通常是可压缩的,所以它可能工作得很好。是的,如果你有数百万个文件,rsync确实需要一段时间,但是对于数千个大文件,它工作得很好。伙计们,我的队友是bash之王,我们通过使用bash脚本和rsync解决了这个问题。在prod中工作就像做梦一样。赢了这么多!我一直在到处寻找解决办法-t是如此优雅,并与顶级的东西太!谢谢:)来自ssh手册页。“-t强制伪tty分配。这可用于在远程机器上执行任意基于屏幕的程序”我刚刚尝试过,但似乎根本不起作用;自2015年以来未维持。浪费$3.Hi@stephan.com。我制作了这个应用程序。一个更新即将到来,不会再花费你的钱了。
     -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.
    
    $pid = fork();
    if(!$pid)
    {
      exec("ssh $host -f 'tail -f $filename |nc $localhost $port'");
      exit;
    }
    
    exec("nc -l -p $port");
    
    #!/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