Categories
Coding Unix

SFTP in Perl Connecting to Legacy Server

We had a client that had a need to do automated SFTPs to various sites. It turns out that some of the sites had an older SSHD that would generate an error like “DH Group Exchange reply out of range”, and that required this parameter if you were using SFTP from the command line:

-oKexAlgorithms=diffie-hellman-group1-sha1

Now in Perl we were using Net::SFTP and it took some finagling (yes, that’s really a word) to figure out the proper settings, so here they were in case you were having trouble. And we had to add the warn flag otherwise we would get this warning:

Couldn’t fsetstat: Permission denied

Another oddity was that when we were putting files, the SFTP server allowed multiple files with the same name! Very odd, so we had to make sure to add a $sftp->do_remove command prior to the put command.

Here is what we used for the function call:

%sftp_args =
( “user” => “myuser”,
“password” => “mypass”,
“debug” => 0,
“warn” => 0,
“ssh_args” => [ options => [ “KexAlgorithms diffie-hellman-group1-sha1” ] ]
);

$sftp = Net::SFTP->new($sftp_host,%sftp_args);