Chinaunix首页 | 论坛 | 博客
  • 博客访问: 486634
  • 博文数量: 142
  • 博客积分: 4126
  • 博客等级: 上校
  • 技术积分: 1545
  • 用 户 组: 普通用户
  • 注册时间: 2008-02-22 10:03
文章分类

全部博文(142)

文章存档

2011年(8)

2010年(7)

2009年(64)

2008年(63)

我的朋友

分类:

2008-12-19 10:17:11

#!/usr/bin/perl
#批量修改服务器密码的脚本

use strict;
use warnings;
use Net::SSH::Expect;

my $user = 'aaa';
my $pass = '123456';
my $root_pass = 'abcdef';
my $new_user_pass = '654321';
my $port = '22';

my $file = '/home/aaa/iplist';
open FILE, "< $file" or die "can't open file $file ($!)";

while (<FILE>) {
    print $_;
    &ssh_test( "$_", "$port", "$user", "$pass" );
}

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,
    );

    open FH, ">> /home/aaa/log_$host" or die $!;

    print FH "-" x 80, "\n";
    print FH "start \tat ${\(scalar localtime() ) }\n";

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

    $ssh->waitfor( '\(yes\/no\)\?$', 6 );
    $ssh->send("yes\n");
    $ssh->waitfor( 'password:\s*$/', 6 );
    $ssh->send("$pass");

    $ssh->send("su - root");
    $ssh->waitfor( 'Password:\s*$', 6 );
    $ssh->send("$root_pass");
    $ssh->waitfor( '#\s*', 6 );

    print FH "root login ok. \n";

    $ssh->send("passwd $user");

    $ssh->waitfor( 'password:\s*$', 6 );
    $ssh->send("$new_user_pass");

    $ssh->waitfor( 'password:\s*$', 6 );
    $ssh->send("$new_user_pass");
    $ssh->waitfor( '#\s*', 6 );

    my $ls = $ssh->exec("id");
    print FH "$ls\n";
    print FH "chang password ok!!!!!!!\n";

    my $end_time = localtime;
    print FH "end \tat $end_time\n";

    $ssh->close();

    close FH;

    print "-" x 30, "\n";
}

阅读(557) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~