- 论坛徽章:
- 0
|
- #!/usr/bin/env perl
- use strict;
- use warnings;
- use IO::Pty;
- use IO::Select;
- # use Term::ReadKey;
- usage() if ($#ARGV < 1);
- my $pty = IO::Pty->new();
- my $select = IO::Select->new($pty);
- my $buffer = "";
- my $pid = fork();
- die "Fork fail!\n" if (not defined $pid);
- if ($pid != 0) {
- my $loop = 1;
- local $SIG{CHLD} = sub { $loop = 0; };
- while ($loop) {
- expect();
- }
- expect();
- waitpid($pid, 0);
- }
- else {
- my $slave = $pty->slave();
- open(STDIN, "<&", $slave);
- open(STDOUT, ">&", $slave);
- open(STDERR, ">&", $slave);
- close $pty;
- close $slave;
- exec("passwd");
- exit 1;
- }
- ################################
- sub usage {
- print STDERR "$0 Password NewPassword\n";
- exit 0;
- }
- sub expect {
- my ($r, $w, $e) = IO::Select->select($select, $select, undef, 0.02);
- if ($#$r > -1) {
- if (sysread($pty, my $buf, 8196)) {
- $buffer .= $buf;
- syswrite(STDOUT, $buf);
- }
- }
- else {
- return;
- }
-
- if ($#$w > -1) {
- if ($buffer =~ /\(current\) UNIX password:/) {
- $buffer = "";
- syswrite($pty, "$ARGV[0]\r");
- }
- elsif ($buffer =~ /New UNIX password:/) {
- $buffer = "";
- syswrite($pty, "$ARGV[1]\r");
- }
- elsif ($buffer =~ /Retype new UNIX password:/) {
- $buffer = "";
- syswrite($pty, "$ARGV[1]\r");
- }
- else {
- ;
- }
- }
- }
复制代码 |
|