Perl实现远程上传脚本并执行
1#!/usr/bin/perl
2#Author:luyafei
3#Email:chulianwang@qq.com
4#The script used as upload script,and execution;
5use strict;
6use warnings;
7use Net::SCP::Expect;
8use Expect;
10sub 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};
18sub all_host{
19my $host = shift ;
20my $pass = shift ;
22$ENV{TERM} = "vt100";
23my $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}
47my @laird = ('192.168.1.3','192.168.1.4');
48for my $i (@laird){
50 yy($i,"luyafei");
51 all_host($i,"luyafei");
53}
希望大家多提建议,帮我更好的改进。
页:
[1]