rajadurai has asked for the wisdom of the Perl Monks concerning the following question:
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"); }
|
|---|