perl - 如何跟踪远程文件?

标签 perl scripting ssh tail netcat

我正在尝试找到一种在远程主机上跟踪文件的好方法。这是在 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 $_;
}

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

最佳答案

你试过吗

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.

关于perl - 如何跟踪远程文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/566110/

相关文章:

javascript - 此处文档中的 JQuery 不起作用

linux - 在 2 组/文件之间提取唯一值

linux - BASH 脚本 'sleep' 命令不起作用

linux - 当 Openldap 服务器上的 slapd 守护进程停止时,Openldap 客户端计算机挂起

c# - 在不使用自定义组件的情况下,使用SFTP连接到远程服务器并下载文件是否可行?

perl - 查找数字,并删除与此数字相等的相邻字符

perl - 我如何使用 Perl 的 Getopt::Long 处理 -r=<pattern>?

perl - 单个键的哈希中的多个值

linux - 为什么 cron 执行的部分脚本会失败,除非将 stderr 定向到/dev/null?

windows - SSH要求输入用户密码而不是ssh key 密码?