Solved

perl - sftp

Posted on 2014-04-13
6
2,255 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 26

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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 26

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 26

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
add some character at the end of line in vi 7 78
Hacked File Timestamps 4 61
wipe a usb using python 5 44
Coding C# in Linux 8 60
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
The viewer will learn how to count occurrences of each item in an array.

813 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now