Solved

perl - sftp

Posted on 2014-04-13
6
2,042 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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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 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.
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…

706 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

17 Experts available now in Live!

Get 1:1 Help Now