如何拖尾远程文件?

Fro*_*sty 37 ssh scripting perl tail netcat

我试图找到一个在远程主机上拖尾文件的好方法.这是在Linux机器的内部网络上.要求是:

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

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

  3. 可以通过Perl调用.

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

我尝试过的解决方案通常都属于这种类型

ssh remotemachine -f <some command>
Run Code Online (Sandbox Code Playgroud)

"一些命令"已经:

tail -f logfile
Run Code Online (Sandbox Code Playgroud)

基本尾部不起作用,因为远程进程在本地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 $_;
}
Run Code Online (Sandbox Code Playgroud)

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

inn*_*naM 61

你有没有尝试过

ssh -t remotemachine <some command>
Run Code Online (Sandbox Code Playgroud)

-sh选项来自ssh手册页:

 -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.
Run Code Online (Sandbox Code Playgroud)

代替

 -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.
Run Code Online (Sandbox Code Playgroud)