Andrew Gaffney wrote:
I've read through the Expect.pm documentation and looked at what few examples I could find, but I still can't figure out how to use Expect to interact with the 'passwd' program. Can anyone point me to an example that does this? Thanks.

I figured it out, mostly. I can get it to work on the command line, but not in CGI.


command line version:

<code>
#!/usr/bin/perl

use Expect;

my $user = shift;
my $password = shift;
print "Running 'sudo passwd ${user}'\n";
my $exp = Expect->spawn("sudo passwd ${user}") or die "Can't run 'sudo passwd 
${user}'\n";
$exp->expect(1000,
             [qr'New UNIX password: $',
              sub {
                my $fh = shift;
                $fh->send("${password}\n");
                print "sent '${password}'\n";
                exp_continue;
              }
             ],
             [qr'Retype new UNIX password: $',
              sub {
                my $fh = shift;
                $fh->send("${password}\n");
                print "sent '${password}'\n";
#                exp_continue;
              }
             ]);
</code>

and the output is:

Running 'sudo passwd agaffney'
New UNIX password: sent '*****'

BAD PASSWORD: it is too short
Retype new UNIX password: sent '*****'

and the CGI version:

<code>
#!/usr/bin/perl

use Expect;
use CGI;

my $cgi = new CGI;
my $p = $cgi->Vars;

print $cgi->header;
my $exp = Expect->spawn("sudo passwd $p->{user}") or die "Can't run 'sudo passwd $p->{user}'\n";
print "Running 'sudo passwd $p->{user}' with password '$p->{password}'<br>";
$exp->expect(1000,
[qr'New UNIX password: $',
sub {
my $fh = shift;
$fh->send("$p->{password}\n");
print "Sent '$p->{password}'<br>";
exp_continue;
}
],
[qr'Retype new UNIX password: $',
sub {
my $fh = shift;
$fh->send("$p->{password}\n");
print "Sent '$p->{password}'<br>";
# exp_continue;
}
]);
</code>


and the output is:

Running 'sudo passwd agaffney' with password '*****'
New UNIX password: New UNIX password: New UNIX password: passwd: Conversation error

Why doesn't the CGI version work? Any help is greatly appreciated.

--
Andrew Gaffney
Network Administrator
Skyline Aeronautics, LLC.
636-357-1548


-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>




Reply via email to