Solved

perl - sftp

Posted on 2014-04-13
6
2,502 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
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 
LVL 27

Accepted Solution

by:
skullnobrains earned 500 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Can't connect to FTP 17 93
Scan Network and Export Reg value to single file 2 47
Advice on ESXi 5.1 Health / Storage 1 40
Permission on folders 6 20
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

713 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