Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

perl - sftp

Posted on 2014-04-13
6
Medium Priority
?
3,315 Views
Last Modified: 2014-04-15
I need a perl script which connects to SFTP and download all files from a folder then it will clean up the folder.

Is it possible ?
0
Comment
Question by:F4SX
  • 3
  • 3
6 Comments
 
LVL 27

Expert Comment

by:skullnobrains
ID: 39997446
i don't have a perl interpreter around with sftp libs, but from the top of my head and a short googling, this should help you get going

$sftp = Net::SFTP::Foreign->new($host, user => $username, password => $password);

$sftp->lcd(LOCAL_PATH);
$sftp->cd(REMOTE_PATH);

my $files = $sftp->ls(); # not sure about parenthesis

foreach (@$files){
  $sftp->get($_,$_)
  and $sftp->remove($_);
}

Open in new window


you'll need to add better error handling and do some testing.
the "and" should make sure that only successfully downloaded files are removed.
0
 

Author Comment

by:F4SX
ID: 40000721
your code returned some error so i try this :

!/usr/bin/perl

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

my $sftp = Net::SFTP::Foreign->new(
    'admin@10.20.30.2',
    password => '',
    more     => [-o => 'StrictHostKeyChecking no','-v']
);

$sftp->get('/log', '')
  or die "unable to retrieve copy: ".$sftp->error;

Open in new window


this time it returned that

OpenSSH_5.3p1, OpenSSL 1.0.1e-fips 11 Feb 2013
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: Applying options for *
debug1: Connecting to 10.20.30.2 [10.20.30.2] port 22.
debug1: Connection established.
debug1: permanently_set_uid: 0/0
debug1: identity file /root/.ssh/identity type -1
debug1: identity file /root/.ssh/identity-cert type -1
debug1: identity file /root/.ssh/id_rsa type -1
debug1: identity file /root/.ssh/id_rsa-cert type -1
debug1: identity file /root/.ssh/id_dsa type -1
debug1: identity file /root/.ssh/id_dsa-cert type -1
debug1: Remote protocol version 2.0, remote software version ROSSSH
debug1: no match: ROSSSH
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_5.3
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: server->client aes128-cbc hmac-md5 none
debug1: kex: client->server aes128-cbc hmac-md5 none
debug1: SSH2_MSG_KEX_DH_GEX_REQUEST(1024<1024<8192) sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_GROUP
debug1: SSH2_MSG_KEX_DH_GEX_INIT sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_REPLY
debug1: Host '10.20.30.2' is known and matches the DSA host key.
debug1: Found key in /root/.ssh/known_hosts:1
debug1: ssh_dss_verify: signature correct
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: SSH2_MSG_SERVICE_REQUEST sent
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentication succeeded (none).
debug1: channel 0: new [client-session]
debug1: Entering interactive session.
debug1: Sending environment.
debug1: Sending env LANG = en_US.UTF-8
debug1: Sending subsystem: sftp

Open in new window

0
 

Author Comment

by:F4SX
ID: 40000822
I realize that the code i send working in reality. When i put a timeout option i see that if the file is still writing by the system it is not able to download it. that is good but i need to know one more thing how should i call the script as this :

perl test.pl username password serverip remotefolder localfolder

if you help me for this also i will be appricate for that
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 27

Accepted Solution

by:
skullnobrains earned 2000 total points
ID: 40000913
admin and passwords are hard-coded in the script so specifying them on the command line is meaningless

---

if you want to read arguments, you'll need to change the script to something like this :

!/usr/bin/perl

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

$username=$ARGV[0];
$password=$ARGV[1];
$host=$ARGV[2];
$cd=$ARGV[3];
$lcd=$ARGV[4];

my $sftp = Net::SFTP::Foreign->new(
    $username.'@'.$host,
    password => $password,
    more     => [-o => 'StrictHostKeyChecking no','-v']
);

...

Open in new window

0
 

Author Closing Comment

by:F4SX
ID: 40001078
thanks
0
 
LVL 27

Expert Comment

by:skullnobrains
ID: 40001177
feel free to post the final code. it is a frequent demand with scp/sftp and the likes and answering it with a simple perl script is quite convenient

note that personally i'd synthetise arguments as urls so you would use something like

sftpmv sftp://user:pass@host/path/to/source/file /path/to/dest
or
sftpmv -r sftp://user:pass@host/path/to/source/directory /path/to/dest
or the same commands with the remote as the destination
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
This article will show, step by step, how to integrate R code into a R Sweave document
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Six Sigma Control Plans
Suggested Courses

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question