在远程服务器上使用perl脚本并将输出恢复到本地终端或屏幕(use perl script on remote servers and bring back output to local terminal or screen)

我有以下perl脚本,在给定输入参数的情况下本地工作。 鉴于我已经成功设置了ssh密钥,我需要脚本访问远程服务器以获取相同的信息。 远程服务器上的日志文件路径与本地相同。 远程服务器的配置完全相同。 我只需要跨多个服务器运行并将数据带回终端或文件。 我需要将它放在shell脚本中吗?

# usage example: <this script> Jun 26 2010 <logfile> use strict; use warnings; my ($mon,$day,$year) = ($ARGV[0],$ARGV[1],$ARGV[2]); open(FH,"< $ARGV[3]") or die "can't open log file $ARGV[3]: $!\n"; while (my $line = <FH>) { if ($line =~ /.* $mon $day \d{2}:\d{2}:\d{2} $year:.*(ERROR:|backup-date=|host=|backup-size=|backup-time=|backup-status)/) { print $line; } }

I have the following perl script that works locally given the input parameters. I need the script to access remote servers for the same information, given that I've already setup the ssh keys successfully. The path for the logfiles on the remote servers are identical to local. Configuration for remote servers are identical. I just need to run across multiple servers and bring back the data either to terminal or on a file. Do I need to put this in a shell script?

# usage example: <this script> Jun 26 2010 <logfile> use strict; use warnings; my ($mon,$day,$year) = ($ARGV[0],$ARGV[1],$ARGV[2]); open(FH,"< $ARGV[3]") or die "can't open log file $ARGV[3]: $!\n"; while (my $line = <FH>) { if ($line =~ /.* $mon $day \d{2}:\d{2}:\d{2} $year:.*(ERROR:|backup-date=|host=|backup-size=|backup-time=|backup-status)/) { print $line; } }

最满意答案

您可以修改脚本以将服务器名称作为额外参数。

# usage example: <this script> Jun 26 2010 <server> <logfile> use strict; use warnings; my($mon,$day,$year,$server,$file) = @ARGV; open(my $fh,"ssh $server cat $file |") or die "can't open log $server:$file: $!\n"; while (my $line = <$fh>) { if ($line =~ /.* $mon $day \d{2}:\d{2}:\d{2} $year:.*(ERROR:|backup-date=|host=|backup-size=|backup-time=|backup-status)/) { print $line; } }

我的版本利用了Perl open函数可以“打开”命令的事实,命令的输出作为脚本的输入。

----编辑

关于你的后续问题,如果文件存在于多个主机上的相同位置,那么你可以交换参数顺序并在命令行上传递主机列表:

# usage example: <this script> Jun 26 2010 <logfile> <server> ... use strict; use warnings; my($mon,$day,$year,$file) = @ARGV; splice(@ARGV, 0, 4, ()); # Discard first 4 args foreach my $server ( @ARGV ) { open(my $fh,"ssh $server cat $file |") or die "can't open log $server:$file: $!\n"; while (my $line = <$fh>) { if ($line =~ /.* $mon $day \d{2}:\d{2}:\d{2} $year:.*(ERROR:|backup-date=|host=|backup-size=|backup-time=|backup-status)/) { print $line; } } close($fh); }

You could modify the script to take the server name as an extra argument.

# usage example: <this script> Jun 26 2010 <server> <logfile> use strict; use warnings; my($mon,$day,$year,$server,$file) = @ARGV; open(my $fh,"ssh $server cat $file |") or die "can't open log $server:$file: $!\n"; while (my $line = <$fh>) { if ($line =~ /.* $mon $day \d{2}:\d{2}:\d{2} $year:.*(ERROR:|backup-date=|host=|backup-size=|backup-time=|backup-status)/) { print $line; } }

My version takes advantage of the fact that the Perl open function can 'open' a command and the output from the command is presented as input to your script.

---- edit

Regarding your follow-up question, if the file exists in the same place on a number of hosts then you could swap the argument order around and pass the list of hosts on the command-line:

# usage example: <this script> Jun 26 2010 <logfile> <server> ... use strict; use warnings; my($mon,$day,$year,$file) = @ARGV; splice(@ARGV, 0, 4, ()); # Discard first 4 args foreach my $server ( @ARGV ) { open(my $fh,"ssh $server cat $file |") or die "can't open log $server:$file: $!\n"; while (my $line = <$fh>) { if ($line =~ /.* $mon $day \d{2}:\d{2}:\d{2} $year:.*(ERROR:|backup-date=|host=|backup-size=|backup-time=|backup-status)/) { print $line; } } close($fh); }

更多推荐