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

Hi,
I have a strange problem. When I run this in the debugger, I see a successful su to the passed user. But when I run this without the debugger, I get the message "Authentication Failed".

I used Net::SSH::Expect for the $ssh.

This is my code to connect and do a su user.

sub su_login { my ($self, $user, $pass) = @_; my $ssh = $self->ssh(); my $log = $self->log(); my $cmd = ''; if($user eq 'root') { $cmd = 'su -'; } else { $cmd = "su $user"; } $ssh->send($cmd); $ssh->waitfor('Password:\s*\z', 3) or return (undef, "Cannot su a +s $user"); $ssh->send($pass); my $output = $ssh->exec('whoami'); $log->message($output); if ($output =~ /sorry|failure/i ) { $log->error("Cannot su as $user"); return (undef, "Failed to login as $user"); } else { $log->message("Successfully logged in as $user"); } return ($ssh, "su $user successful"); }
  • Comment on Script works in debugger, but fails when run (using Net::SSH::Expect)
  • Download Code