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

I have a script that works fine (with no intervention) for changing Linux passwords except when the remote system's authorized_keys file does not have an entry for our password depot server (where the script is run from). The script then halts and prompts for a password from the command line. Is there a way around this?...without forking a process. I have tried to use POE for non-blocking but I cannot get that to work either.

 sub ChangeRemoteServerPassword
 {
   my ($server, $adminuser, $newpass) = @_;
   my $ssh = Net::OpenSSH->new($server);
   if ($ssh->error)
   {
      Logit(*DBLOG,"FAILED: Remote SSH connection - $ssh->error\n");
      return (0);
   }
   my $userstr = $adminuser . "\:" . "$newpass";
   my $cmd = "echo \'$userstr\' | /usr/sbin/chpasswd";
   $ssh->system("$cmd");
   if ($ssh->error)
   {
      Logit(*DBLOG,"FAILED: Running SSH cmd - $ssh->error\n");
      return (0);
   }
   return 1;
 }

Replies are listed 'Best First'.
Re: Prompted for password in OpenSSH
by salva (Canon) on Apr 19, 2011 at 14:21 UTC
    my $ssh = Net::OpenSSH->new($server, master_opts => [-o => 'BatchMode=yes']);
      Thanks salva!!!...works great
Re: Prompted for password in OpenSSH
by fidesachates (Monk) on Apr 19, 2011 at 13:56 UTC
    AFAIK, there is no way around this utilizing any of the facets of ssh. However, you could easily reach a solution using Expect. I've always dealt with Expect directly (i.e. writing an Expect script), however I know there have been many occasions where the cpan module for Expect has been recommended. Take a look.

    Clarification: I am speaking about entering in the password when you receive that prompt so that you can log in and continue your script