Rex - 远程执行:
[root@node01 my-first-rex-project]# cat Rexfile
use Rex -feature => ['1.0'];
no strict;
use Rex::Misc::ShellBlock;
use Rex::Misc::ScanBlock;
user "root";
password "1234567";
desc "Show Unix version";
task "uname", sub {
say run "ifconfig -a | grep inet";
};
[root@node01 my-first-rex-project]# rex uname
[2017-05-03 20:51:29] INFO - Running task uname on <local>
inet addr:192.168.137.2 Bcast:192.168.137.255 Mask:255.255.255.0 inet6 addr: fe80::20c:29ff:fe29:8eac/64 Scope:Link inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1/128 Scope:Host
[2017-05-03 20:51:30] INFO - All tasks successful on all hosts
[root@node01 my-first-rex-project]#
不 task "uname", sub {
不指定group 统计的是本机:
统计远程机器:
[root@node01 my-first-rex-project]# cat Rexfile
use Rex -feature => ['1.0'];
no strict;
use Rex::Misc::ShellBlock;
use Rex::Misc::ScanBlock;
user "root";
password "1234567";
group myservers => "192.168.137.3";
desc "Show Unix version";
task "uname", group => "myservers", sub {
say run "ifconfig -a | grep inet";
};
[root@node01 my-first-rex-project]# rex uname
[2017-05-03 20:54:47] INFO - Running task uname on 192.168.137.3
inet addr:192.168.137.3 Bcast:192.168.137.255 Mask:255.255.255.0 inet6 addr: fe80::20c:29ff:fe14:8fc1/64 Scope:Link inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1/128 Scope:Host
[2017-05-03 20:54:58] INFO - All tasks successful on all hosts
[root@node01 my-first-rex-project]#
类方法
get_current_connection
[root@node01 my-first-rex-project]# cat Rexfile
use Rex -feature => ['1.0'];
no strict;
use Rex::Misc::ShellBlock;
use Rex::Misc::ScanBlock;
user "root";
password "1234567";
group myservers => "192.168.137.3";
desc "Show Unix version";
task "uname", group => "myservers", sub {
say run "ifconfig -a | grep inet";
my $rhash=Rex::get_current_connection();
print %{$rhash};
print "
";
};
[root@node01 my-first-rex-project]# rex uname
[2017-05-03 21:02:48] INFO - Running task uname on 192.168.137.3
inet addr:192.168.137.3 Bcast:192.168.137.255 Mask:255.255.255.0 inet6 addr: fe80::20c:29ff:fe14:8fc1/64 Scope:Link inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1/128 Scope:Host
notifyRex::Notify=HASH(0x254fdd0)cacheRex::Interface::Cache::Base=HASH(0x254fe30)server192.168.137.3taskARRAY(0x254fe90)sshprofilerRex::Profiler=HASH(0x254fc38)connRex::Interface::Connection::OpenSSH=HASH(0x254ff38)reporterRex::Report::Base=HASH(0x254fea8)
[2017-05-03 21:03:00] INFO - All tasks successful on all hosts
[root@node01 my-first-rex-project]# rex uname | grep '192.168.137.3'
[2017-05-03 21:05:25] INFO - Running task uname on 192.168.137.3
inet addr:192.168.137.3 Bcast:192.168.137.255 Mask:255.255.255.0 inet6 addr: fe80::20c:29ff:fe14:8fc1/64 Scope:Link inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1/128 Scope:Host
'name' => '192.168.137.3',
'_host' => '192.168.137.3',
'_host_squared' => '192.168.137.3',
'_ctl_path' => '/root/.libnet-openssh-perl/root-192.168.137.3-2-8089-364094',
'name' => '192.168.137.3',
[2017-05-03 21:05:37] INFO - All tasks successful on all hosts