perl脚本将文件上传至服务器,Perl实现远程上传脚本并执行

  • Post author:
  • Post category:其他


前面懒人之脚本配置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 }

希望大家多提建议,帮我更好的改进。