Saturday, October 8, 2011

How to Change Linux Password via SSH

Script to change Linux password remotely via SSH.

Prerequisite
For ActivePerl users, you need to download the Net::SSH2 package. Version 5.10.x

C:\>ppm install http://cpan.uwinnipeg.ca/PPMPackages/10xx/Net-SSH2.ppd
Version 5.12.x
C:\>ppm install http://cpan.uwinnipeg.ca/PPMPackages/12xx/Net-SSH2.ppd

Scripts

#!/usr/bin/perl 

use strict;
use Net::SSH2;

use Getopt::Long;

my %option = (
    'server'       => 'localhost',
    'userid'       => '',
    'password'     => '',
    'newpassword'  => '',
);

if (!GetOptions("server|s=s"       => \$option{'server'},
                "userid|u=s"       => \$option{'userid'}, 
                "password|p=s"     => \$option{'password'}, 
                "newpassword|n=s"  => \$option{'newpassword'})) {
    print_usage();  
}

my $ssh2 = Net::SSH2->new();

$ssh2->connect($option{'server'}) or die "Unable to connect Host $option{'server'} \n";

$ssh2->auth_password($option{'userid'}, $option{'password'}) or die "Unable to login\n";

my $chan = $ssh2->channel();
$chan->blocking(0);
$chan->ext_data('merge');

my $cmd = "chage -l $option{'userid'}; " 
        . "echo -e \"$option{'password'}\\n$option{'newpassword'}\\n$option{'newpassword'}\\n\" | passwd 2>&1\n";

# print $cmd;

$chan->exec($cmd);

while (<$chan>) { 
    print $_;
} 

#########################################################################
# Subroutines
#########################################################################
sub print_usage {
    # ...
}

1 comment:

  1. Lucky Numbers | The JamBase! | Missouri City | KC-M-D - JT Hub
    The JamBase! is located in the heart 거제 출장마사지 of 논산 출장안마 the 동두천 출장안마 Missouri River State, and features two slot 논산 출장마사지 machines. The slot machine is one of our favorite 의왕 출장마사지

    ReplyDelete