Solved

perl - sftp

Posted on 2014-04-13
6
2,406 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
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 
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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Suggested Solutions

It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

856 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