jcpunk has asked for the wisdom of the Perl Monks concerning the following question:

there has to be a problem with how i am passing the password to my expect object, i know that i have the correct password (just reset it) . i keep getting a authentication failed error from su. what have i missed?
#!/usr/local/bin/perl -w use strict; use Expect; my $username = $ARGV[0]; my $password = $ARGV[1]; my $on_off = $ARGV[2]; my $message; my $set_fwd; my $timeout = 15; my $exp = new Expect; my $FILENAME; my @list_of_files; @list_of_files=(".forward", ".vacation.msg", ".vacation.pag", ".vacati +on.dir"); if($#ARGV > 2) { $message = join (" ",@ARGV); $message =~ s/$username//; $message =~ s/$password//; $message =~ s/$on_off//; } ####################################### print $username; print "\n"; print $password; print "\n"; print $on_off; print "\n"; print $message; print "\n#######################################\n"; ####################################### $exp->raw_pty(1); $exp->spawn("su - $username") or die "Cannot Begin Login Process\n";; $exp->expect($timeout, [ 'assword', sub { $exp->send("$password"); exp_continue; } ], [ '-re', qr'[#>:%] $', sub { if($on_off eq "on") { foreach $FILENAME (@list_of_files) { $exp->send("/bin/touch /home/$username/$FILENAME"); if ( $FILENAME eq ".forward") { #.forward must look like \username, "|/usr/bin/ +vacation username" $set_fwd="\\$username, \"|/usr/bin/vacation $usern +ame\""; $exp->send("/bin/echo $set_fwd > /home/$username/$ +FILENAME"); } if ( $FILENAME eq ".vacation.msg") { $message .= '\n'; $exp->send("/bin/echo $message > /home/$username/$ +FILENAME"); } $exp->send("/bin/chmod 644 /home/$username/$FILENAME" +); $exp->send("/bin/chown $username /home/$username/$FIL +ENAME"); } if($on_off eq "off") { foreach $FILENAME (@list_of_files) { $exp->send("/bin/rm -f /home/$username/$FILENAME"); } } exp_continue; } } ], [ timeout => sub { print "Process Timed Out.\n"; } ] );
anyone know what to do to fix this?

Replies are listed 'Best First'.
Re: Expect module & su
by naChoZ (Curate) on Jun 02, 2003 at 17:06 UTC
    Does the perl module automatically add the \r? Although I did it in a native expect script, one thing I did was instead of passing the root pw to the script, which seemed a little icky to me, I did this:

    send -- "su\r" interact -nobuffer "\r" return
    This simply goes into interactive mode without collecting the root pw in the script.

    ~~
    naChoZ

Re: Expect module & su
by jkenneth (Pilgrim) on Jun 02, 2003 at 18:41 UTC
    The output for the password prompt goes to stderr, not stdout. I don't have time to get a fix working but hopefully this points you in the right direction.

    JK
    Update:
    The comment about the newline was right. Add a \n in the send and things should be ok.

    JK
Re: Expect module & su
by logan (Curate) on Jun 02, 2003 at 18:46 UTC
    How is the authentication failing? Is su failing, or is the Expect call timing out? Is there a newline attached to $exp->send("$password");? Is Expect failing to detect the prompt that signifies success? Try setting $exp->debug(3) to see what's actually being received by your code.

    -Logan
    "What do I want? I'm an American. I want more."

Re: Expect module & su
by bobn (Chaplain) on Jun 03, 2003 at 01:41 UTC
    This probably isn't your actual problem, but it is a flakey waiting to happen, in that metacharacters in your arguments may make the substituitions do more than you expect. This:
    if($#ARGV > 2) { $message = join (" ",@ARGV); $message =~ s/$username//; $message =~ s/$password//; $message =~ s/$on_off//; }
    would be better as:
    if($#ARGV >2 ) { $message = join (" ",@ARGV[3..$#ARGV]); }
    Either that or use e.g. quotemeta($password) in the first part of your substitutions.

    --Bob Niederman, http://bob-n.com