File: change_passwd.pl

package info (click to toggle)
libnet-openssh-perl 0.84-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 448 kB
  • sloc: perl: 3,410; makefile: 2
file content (52 lines) | stat: -rw-r--r-- 1,060 bytes parent folder | download | duplicates (7)
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
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/usr/bin/perl

use strict;
use warnings;

use Net::OpenSSH;
use Expect;

select STDOUT; $| = 1;
select STDERR; $| = 1;

@ARGV == 3 or die <<USAGE;
Usage:
  $0 host old_password new_password
USAGE

my ($host, $old, $new) = @ARGV;

my $timeout = 20;
my $debug = 0;

my $ssh = Net::OpenSSH->new($host, password => $old);

my ($pty, $pid) = $ssh->open2pty("passwd")
    or die "open2pty failed: " . $ssh->error . "\n";

my $expect = Expect->init($pty);
$expect->raw_pty(1);
$debug and $expect->log_user(1);

sub answer_passwd {
    my ($pattern, $pass) = @_;

    $debug and print "waiting for password prompt\n";
    $expect->expect($timeout, -re => $pattern)
        or die "expect failed\n";
    $debug and  print "prompt seen\n";

    $expect->send("$pass\n");
    $debug and print "password sent\n";

    $expect->expect($timeout, "\n")
        or die "bad password\n";
}

answer_passwd('current.*:', $old);
answer_passwd('new.*:', $new);
answer_passwd('new.*:', $new);

$expect->expect($timeout, "success") or die "Failed!\n";

print "password updated\n";