We help IT Professionals succeed at work.

Existing perl code to be changed for ftp to sftp handling

srinivasa rao
on
I need a solution to change the ftp stuff to the sftp in the perl file attached.
please find the code snippet below as well.ftpget.txt

while($versionfileflag eq 'true'){    # while loop for polling begins here .........
open (REMOTEFILELIST,"|ftp -n -i > $WorkingDir/remotefilelist\.$SiteId") || die "Cannot open remote file list connection: $!\n";
print REMOTEFILELIST "open $FTPServer\n";
print REMOTEFILELIST "user $FTPUser $FTPPasswd\n";
print REMOTEFILELIST "cd $RemoteDir\n";
print REMOTEFILELIST "ls -l\n";
print REMOTEFILELIST "quit\n";
close REMOTEFILELIST || print STDERR "Cannot close remote file list connection: $!";
Comment
Watch Question

You'll need to replace that block of code with something like this:
use Net::SFTP;
my $sftp = Net::SFTP->new($FTPServer, { user => $FTPUser, password => $FTPPasswd }) or die "Cannot open remote file list connection: $!";
my @files;
$sftp->ls($RemoteDir, sub { push @files, $_->{longname} });
open FIL, ">$WorkingDir/remotefilelist\.$SiteId" or die "could not write $WorkingDir/remotefilelist\.$SiteId: $!";
print FIL join("\n", @files), "\n";
close FIL;

Open in new window

Net::SFTP hasn't been updated since 2005 and it also requires a lot of dependencies such as Net::SSH::Perl which is a real pain to install especially on Windows.

The preferred module choice is Net::SFTP::Foreign.

Your current script is using password authentication, which both of these modules support, but the preferred auth method is to use ssh keys.  Passing plain text passwords as you're currently doing is insecure and should be avoided.
Net::SFTP hasn't been updated since 2005 but it works flawlessly.  However, I did just try installing it on Windows and apparently Math::Pari (a dependency) does not compile on Windows (no problem on *nix).

It looks like Net::SFTP::Foreign is not quite compatible with Net::SFTP API.  This code should work with Net::SFTP::Foreign.
use Net::SFTP::Foreign;
my $sftp = Net::SFTP::Foreign->new($FTPServer, { user => $FTPUser, password => $FTPPasswd });
$sftp->die_on_error("Cannot open remote file list connection");
my $res = $sftp->ls($RemoteDir);
open FIL, ">$WorkingDir/remotefilelist\.$SiteId" or die "could not write $WorkingDir/remotefilelist\.$SiteId: $!";
print FIL $_->{longname}, "\n" for (@$res);
close FIL;

Open in new window

Author

Commented:
Thank You for the suggestions.
Am trying to install the NET::SFTP::FORIEGN package but facing difficulties with dependencis.

If am going to download perl 5.20 latest then is going to have the net::sftp::foreign included in it.

Author

Commented:
Dear Wilcoxon,

am able to install the package

But now when trying the below code getting the
 error:
C:\Users\dell\Desktop>perl sftp-test.pl
sftp target host not defined at sftp-test.pl line 8.


CODE:

$FTPServer='test.rebex.net';
$FTPUser='demo';
$FTPPasswd='password';
$WorkingDir='C:\Users\dell\Desktop\sftp';
$RemoteDir='C:\Users\dell\Desktop\sftp\remote';
$SiteId='Partner';
use Net::SFTP::Foreign;
my $sftp = Net::SFTP::Foreign->new($FTPServer, { user => $FTPUser, password => $FTPPasswd } );
$sftp->die_on_error("Cannot open remote file list connection");
my $res = $sftp->ls($RemoteDir);
open FIL, ">$WorkingDir/remotefilelist\.$SiteId" or die "could not write $WorkingDir/remotefilelist\.$SiteId: $!";
print FIL $_->{longname}, "\n" for (@$res);
close FIL;
Looks like the optional argument syntax for Net::SFTP::Foreign is different.  Change the connect line to:
my $sftp = Net::SFTP::Foreign->new($FTPServer, user => $FTPUser, password => $FTPPasswd, autodie => 1);

Open in new window

Author

Commented:
Dear Wilcoxon,

when trying to run  to run the code with the above modification, am getting the below error.
Can you please assist.

bash-3.00$ perl sftp-srinu-latest.pl
password authentication not available, IO::Pty is not installed or failed to load: Can't locate IO/Pty.pm in @INC (@INC contains: /usr2/SIR08364/Perl/site/lib /usr2/SIR08364/Perl/lib .) at /usr2/SIR08364/Perl/site/lib/Net/SFTP/Foreign/Backend/Unix.pm line 256.
 at sftp-srinu-latest.pl line 9.
bash-3.00$
bash-3.00$
bash-3.00$ uname -a
SunOS CSAPD02 5.10 Generic_137138-09 i86pc i386 i86pc

Vi -R Unix-pm

256              eval { require IO::Pty; 1 }
257                  or croak "password authentication not available, IO::Pty is not installed or failed to load: $@";
You need to install the IO::Pty module in order to use password authentication.  Without that module you will need to use ssh keys when authenticating.

The same is true when running the script on Windows and in that case installing IO::Pty and using password authentication only works when running perl under cygwin.
Are you running on Windows?  If so, there may be a big problem.  It's looking like it may not be possible to do ssh with password auth using perl on native Windows (Net::SFTP requires Math::Pari and Net::SFTP::Foreign requires IO::Pty - both of which are incompatible with Windows).  Almost any non-Windows-compatible module will work under Cygwin but that's an extra layer to install/use to get it working.

If you're on a different OS, then just install IO::Pty and everything should work.
Does the remote host allow ssh connections or only sftp?  If it allows ssh connections, it should be possible to use Net::SSH2 instead of Net::SFTP* to do what you want.
Apparently there is a working ppm (perl package manager) archive for Net::SFTP::Foreign for Windows.  Try "ppm install Net-SFTP-Foreign-Backend-Net_SSH2".  If ppm is found but you get an error then you may need to do "repo add sisyphusion".  If ppm is not found then you'll need to install.

Author

Commented:
Dear wilcoxon,

Am working in a Active perl 5.16 on Sun solaris.
Let me try with the IO::Pty and check.

Thanks again for ur time and support.

Regards,
Srinivas
Srinivas,

Were you able to get this working, or do you still need help?

If you still need help, please confirm which system you're working on.  Some of your posts indicates that you're running the script on Windows but others indicate you're on Solaris.

There is another person with the same (sftp) problem which I am helping to resolve.  It may be helpful to to to read that thread.
Connecting to linux server using Net::Telnet module  

Here is the tested/working example I posted in that thread.
#!/usr/bin/perl

use warnings;
use strict;
use Net::SFTP::Foreign;
use Data::Dumper;

my $host = '10.100.0.118';
my $sftp = Net::SFTP::Foreign->new($host,
                                   backend => 'Net_SSH2',
                                   user => 'me',
                                   password => 'mypass');
$sftp->die_on_error("Unable to establish SFTP connection");
$sftp->setcwd('/home') or die "unable to change cwd: " . $sftp->error;

my @files = $sftp->ls('.');
print Dumper \@files;

Open in new window


c:\test>Perl-1.pl
$VAR1 = [
          [
            {
              'a' => bless( {
                              'mtime' => 1361376188,
                              'atime' => 1431030184,
                              'perm' => 16877,
                              'flags' => 15,
                              'size' => 4096,
                              'uid' => 0,
                              'gid' => 0
                            }, 'Net::SFTP::Foreign::Attributes' ),
              'filename' => '.',
              'longname' => 'drwxr-xr-x    3 root     root         4096 Feb 20  2013 .'
            },
            {
              'filename' => '..',
              'longname' => 'drwxr-xr-x   24 root     root         4096 May  6 08:59 ..',
              'a' => bless( {
                              'gid' => 0,
                              'uid' => 0,
                              'flags' => 15,
                              'size' => 4096,
                              'atime' => 1431024163,
                              'perm' => 16877,
                              'mtime' => 1430927972
                            }, 'Net::SFTP::Foreign::Attributes' )
            },
            {
              'filename' => 'rkb',
              'longname' => 'drwx--x--x   63 rkb      rkb         12288 May  7 08:54 rkb',
              'a' => bless( {
                              'mtime' => 1431014091,
                              'perm' => 16841,
                              'atime' => 1431017837,
                              'flags' => 15,
                              'size' => 12288,
                              'uid' => 500,
                              'gid' => 500
                            }, 'Net::SFTP::Foreign::Attributes' )
            }
          ]
        ];

Open in new window

Suhas .Senior QA Manager

Commented:
No comment has been added to this question in more than 21 days, so it is now classified as abandoned.

I have recommended this question be closed as follows:

Split:
-- FishMonger (http:#a40766861)
-- wilcoxon (http:#a40747247)


If you feel this question should be closed differently, post an objection and the moderators will review all objections and close it as they feel fit. If no one objects, this question will be closed automatically the way described above.

suhasbharadwaj
Experts-Exchange Cleanup Volunteer