在远程服务器上使用 perl 脚本并将输出返回到本地终端或屏幕
我有以下 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您的 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.您可以修改脚本以将服务器名称作为额外参数。
我的版本利用了这样一个事实:Perl
open
函数可以“打开”命令,并且命令的输出将作为脚本的输入呈现。---- 编辑
关于您的后续问题,如果该文件存在于多个主机上的同一位置,那么您可以交换参数顺序并在命令行上传递主机列表:
You could modify the script to take the server name as an extra argument.
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: