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

I have a script that connects to several systems (linux to linux) via SFTP and checks for the existance of a file. When connecting to one host I get the error "the authenticity of the target host can not be established, connect from the command line first".

I have successfully connected from the command line and the remote host's key is in the known_hosts file." This is happening only on one system. I have tried setting "PreferredAuthentications=password" also with the same results.

Here is the code snippet

sub SFTPTest() { my $whoami=qx(whoami); my $id=qx(id); $protocol = "SFTP"; $Port = $SFTPPort; my $SftpConn = Net::SFTP::Foreign ->new(host=>$HostFQDN, user=>$st +nocuser, password=>$passwd, port=>$Port, timeout=>$TimeOut); my $SftpConnErr = $SftpConn->error; if ($SftpConnErr) { my $SftpConnStatus =$SftpConn->status; $ErrorState = &CheckErrorMsg($SftpConnStatus); return ($ErrorState); } my $SftpConnLsMsg = $SftpConn->ls; $SftpConnErr = $SftpConn->error; if ($SftpConnErr) { my $SftpConnStatus =$SftpConn->status; $ErrorState = &CheckErrorMsg($SftpConnStatus); } $SftpConn->disconnect; return($ErrorState); }

Replies are listed 'Best First'.
Re: Error with Net::SFTP:Foreign
by pileofrogs (Priest) on Aug 11, 2010 at 17:55 UTC

    I haven't used Net::SFTP::Foreign before, but I have run into similar problems.

    Usually this type of problem comes from your script consulting a different known_hosts file than your command line. EG your command line runs as a user and your script runs as root. One option is to insert the host keys for all the hosts in the /etc/ssh/known_hosts file. That way it won't matter which user the script is running as. See ssh-keyscan for details.

      Thanks for the reply. I thought about that issue and in the script I checked the user id I am executing as and it is root. In root's home directory I have the .ssh/known_hosts file with the key in it. If I remove the key and execute the sftp from the command line it detects there is no current key and prompts if I want to add it. I reply with yes and it adds the key. I will try adding the /etc/ssh/known_hosts to see if that helps.

Re: Error with Net::SFTP:Foreign
by Khen1950fx (Canon) on Aug 11, 2010 at 19:53 UTC
    It seems to me that you are connecting to one host, then that connection is disconnected before the next host is called; hence you get "connect from the command line first". One possible solution would be to add the autodisconnect option to %args. For example, here's what I tried:
    #!/usr/bin/perl use strict; use warnings; use Net::SFTP::Foreign; my %args = ( user => 'user', password => 'password', more => '-v', autodisconnect => 0 ); my $sftp = Net::SFTP::Foreign ->new('localhost', %args); $sftp->error and die $sftp->error; $sftp->status or $sftp->error; my $ls = $sftp->ls('/root') or die "Unable to retrieve directory: " . $sftp->error; print "$_->{filename}\n" for (@$ls); $sftp->disconnect;
Re: Error with Net::SFTP:Foreign
by salva (Canon) on Aug 22, 2010 at 09:10 UTC
    Try running ssh in debug mode:
    my $SftpConn = Net::SFTP::Foreign->new(host => $HostFQDN, port => $Po +rt, user => $stnocuser, password => + $passwd, timeout=> $TimeOut, more => '-vvv');
    and see what it says...