在远程服务器上使用 perl 脚本并将输出返回到本地终端或屏幕

发布于 2024-09-06 19:28:51 字数 581 浏览 9 评论 0原文

我有以下 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;
   }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

天气好吗我好吗 2024-09-13 19:28:51

如果您的 Perl 脚本已在远程服务器上就位,只需调用 ssh someserver /path/to/the.script.pl 即可。远程 stdout 和 stderr 通过管道返回给您。

Provided your perl script is already in place on the remote servers, just invoke ssh someserver /path/to/the.script.pl. The remote stdout and stderr are piped back to you.

烟花肆意 2024-09-13 19:28:51

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

# 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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文