微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Perl实现远程上传脚本并执行

 

前面懒人之脚本配置snmp服务 文章提到 ,提到用perl 写非交互远程登录上传安装脚本,并执行。呵呵  用了一天才完成,最后朋友给加了,循环。下面脚本经过测试没问题,这脚本还的改进,我需要从服务器上下载日志到本地。

     1  #!/usr/bin/perl
     2  #Author:luyafei
     3  #Email:chulianwang@qq.com
     4  #The script used as upload script,and execution;
     5  use strict;
     6  use warnings;
     7  use Net::SCP::Expect;
     8  use Expect;
    10  sub yy {
    11          my $host= shift;
    12          my $pass= shift;
    14          my $scpe = Net::SCP::Expect->new(user=>'root',password=>$pass);
    15          $scpe->scp('/home/script/gongzuo/touch.sh',"$host:/tmp/");
    16  };
    18  sub all_host{
    19  my $host = shift ;
    20  my $pass = shift ;
    22  $ENV{TERM} = "vt100";
    23  my $exp = Expect->new;
    24  $exp = Expect->spawn("ssh -l root $host");
    25  $exp->log_file("output.log","w");
    26  $exp->expect(2,[
    27                                  qr/password:/i,28                  sub {
    29                      my $selt = shift ;
    30                                          $selt->send("$pass\n");
    31                                          exp_continue;
    32                                          }
    33                  ],34                  [
    35                                  'connecting (yes/no)?',36                                  sub {
    37                                          my $new_self = shift ;
    38                                          $new_self->send("yes\n");
    39                                          }
    40                                  ]
    42  );
    43  $exp->send("/tmp/touch.sh\n") if ($exp->expect(undef,"#"));
    44  $exp->send("exit\n") if ($exp->expect(undef,"#"));
    45  $exp->log_file(undef);
    46  }
    47  my @laird = ('192.168.1.3','192.168.1.4');
    48  for my $i (@laird){
    50          yy($i,"luyafei");
    51          all_host($i,"luyafei");
    53  }
希望大家多提建议,帮我更好的改进。


版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐