rex 上传文件并远程执行

Rex::Commands::File - Transparent File Manipulation:


名称

Rex::Commands::File - Transparent File Manipulation


task "read-passwd2", "192.168.137.3", sub {
   say cat "/etc/passwd";
 };

[root@node01 Rex]# rex "read-passwd2" | grep tlcb
[2017-09-12 02:16:08] WARN - Please use only the following characters for task names:
[2017-09-12 02:16:08] WARN -   A-Z, a-z, 0-9 and _
[2017-09-12 02:16:08] WARN - Also the task should start with A-Z or a-z
[2017-09-12 02:16:08] WARN - You can disable this warning by setting feature flag: disable_taskname_warning
[2017-09-12 02:16:08] INFO - Running task read-passwd2 on 192.168.137.3
tlcb:x:504:504::/home/tlcb:/bin/bash
[2017-09-12 02:16:09] INFO - All tasks successful on all hosts
[root@node01 Rex]# 

file($file_name, %options)

这个函数是Install file命令的缩写,请使用这个函数来上传文件:

task "prepare01", "192.168.137.3", sub {
   file "/root/sbin/a1.pl",
   source => "/root/Rex/a1.pl";
};
[root@node01 Rex]# rex prepare01

文件从本地的/root/Rex/a1.pl 上传到/root/sbin/a1.pl


task "prepare01", "192.168.137.3", sub {
   file "/home/mqm/xbin/test.sh",
   source => "/root/Rex/test.sh",
   mode => 755,  
   owner  => "mqm",  
   group  => "mqm";
   my $output = run "/home/mqm/xbin/test.sh";  
   say $output;  

};

上传文件,执行命令

node2:/home/mqm/xbin#ls -ltr
total 33548
-rw-r--r-- 1 mqm mqm 34344960 Mar 28  2017 mqm.tar
drwxrwxr-x 3 mqm mqm     4096 Aug 24 22:02 mqm
-rwxr-xr-x 1 mqm mqm       35 Sep 12 02:41 test.sh

权限为755 数组为mqm:mqm


task "prepare01", "192.168.137.3", sub {
   file "/home/mqm/xbin/test.sh",
   source => "/root/Rex/test.sh",
   mode => 700,  
   owner  => "mqm",  
   group  => "mqm";
   my $output = run "/home/mqm/xbin/test.sh";  
   say $output;  

};

node2:/home/mqm/xbin#ls -ltr
total 33548
-rw-r--r-- 1 mqm mqm 34344960 Mar 28  2017 mqm.tar
drwxrwxr-x 3 mqm mqm     4096 Aug 24 22:02 mqm
-rwx------ 1 mqm mqm       35 Sep 12 02:41 test.sh

原文地址:https://www.cnblogs.com/hzcya1995/p/13349493.html