远程控制多台服务器,perl远程执行多台服务器shell命令

#!/usr/bin/perl

use strict;

use Net::SSH::Expect;

my @ssh_list;

my $ssh_txt='ip_list.txt';

my $command_txt='command_txt.txt';

open FH,$ssh_txt;

while(){

@ssh_list=split;

print "正在登陆".$ssh_list[0]."...\n";

&ssh_conn("$ssh_list[0]","$ssh_list[1]","$ssh_list[2]","$ssh_list[3]");

}

close    FH;

sub    ssh_conn(){

my($host,$port,$user,$pass) = @_;

my $ssh = Net::SSH::Expect->new(

host        =>            $host,

port        =>            $port,

user        =>            $user,

password                =>$pass,

no_terminal         =>0,

raw_pty =>1,

timeout =>            3,

);

$ssh->debug(0);

$ssh->run_ssh() or die "SSH process coundn't start:$!";

$ssh->waitfor( '\(yes\/no\)\?$', 1 ); #交互式修改密码,给予2秒的时间

$ssh->send("yes\n");

$ssh->waitfor( 'password:\s*$/', 1);

$ssh->send("$ssh_list[3]");

$ssh->send("su - root");

$ssh->waitfor( 'password:\s*$/', 1);

$ssh->send("$ssh_list[4]");

#$ssh->waitfor("#\s*",2);

open F1,$command_txt;

while(){

my @command=split/\n/,$_;

print "$command[0]-->    ";

$ssh->exec("$command[0]");

print "$ssh_list[0]命令执行完毕\n";

}

close F1;

$ssh->close();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值