首页 > 解决方案 > Perl 在不同的主机上运行子程序

问题描述

我是 perl 的新手。我有脚本,我需要跳转到不同的主机并比较 FS、环境等

我有一个主跳转服务器(MAIN_JUMP 和 5 个跳转服务器到不同的集群(CLUSTER_JUMP_1-5)。我在 MAIN_JUMP 上运行我的脚本,但我需要在 CLUSTER_JUMP_* 上运行一些子例程。在子例程中我跳转到集群中的特定主机。

是否可以通过 ssh 或某些 perl 模块直接在 CLUSTER_JUMP 上运行子程序?现在我使用双 ssh 到 CLUSTER_JUMP_* 然后到特定的主机。它在某些情况下可以正常工作,但例如由于引号,对 oracle 数据库的选择无法正常工作。

标签: perl

解决方案


Object::Remote将以一种非常简单的方式为您完成此操作...

use strict;
use warnings;
use feature 'say';
use Object::Remote;

####################################################################
# Note that My::File must be installed on the machines you want to
# run this on!
####################################################################
# package My::File;
# use Moo;
# has path => ( is => 'ro', required => 1 );
# sub size {
#   my $self = shift;
#   -s $self->path;
# }
# 1;
####################################################################

use My::File;

## find the size of a local file
my $file1 = My::File->new( path => '/etc/hostname' );
say $file1->size;

## find the size of a file on a remote host
my $conn  = Object::Remote->connect('host.example.net');  # ssh 
my $file2 = My::File->new::on( $conn, path => '/etc/hostname' );
say $file2->size;

更新:为清楚起见,“My::File”没有什么特别之处。这只是您将编写并确保在您将远程访问的所有机器以及“客户端”机器上正确安装的模块的一个示例。它可以是任何以 OO 风格编写的模块。


推荐阅读