1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
#!/usr/bin/perl use strict; use warnings; use Net::SSH::Expect; my @ssh_list; while (<>) { @ssh_list=split/\t+/,$_; print $ssh_list[0]."正在修改密码\n"; &ssh_test("$ssh_list[0]","$ssh_list[1]","$ssh_list[2]","$ssh_list[3]","$ssh_list[4]"); } sub ssh_test(){ my ( $host, $port, $user, $pass) = @_; my $ssh = Net::SSH::Expect->new( host => $host, port => $port, password => $pass, user => $user, no_terminal => 0, raw_pty => 1, timeout => 6, ); $ssh->debug(0); $ssh->run_ssh() or die "SSH process couldn't start: $!"; $ssh->waitfor( '\(yes\/no\)\?$', 2 ); $ssh->send("yes\n"); $ssh->waitfor( 'password:\s*$/', 2); $ssh->send("$ssh_list[3]"); $ssh->waitfor( '#\s', 2 ); $ssh->send("passwd $ssh_list[2]"); $ssh->waitfor( 'password:\s*$', 2 ); $ssh->send("$ssh_list[4]"); $ssh->waitfor( 'password:\s*$', 2 ); $ssh->send("$ssh_list[4]"); $ssh->waitfor( '#\s*', 2 ); $ssh->close(); print "修改完成\n"; print "-" x 30, "\n"; } |