#!/usr/bin/perl
use Switch;
use Getopt::Long;
use Net::SFTP::Foreign;
my $status = GetOptions(
"host|h=s" => \$host,
"username|u=s" => \$username,
"password|p=s" => \$password,
"from_dir|f=s" => \$FROM_DIR,
"to_dir|t=s" => \$TO_DIR,
"switch|s=s" => \$fcopy,
);
if ( $status == 0 ) {
print_usage();
exit(3);
}
my $sftp;
$sftp = Net::SFTP::Foreign->new (
$host,
timeout => 240,
user => $username,
password => $password,
autodie => 1,
);
switch ($fcopy)
{
case "folder"
{
$sftp->die_on_error("Unable to establish SFTP connection");
my $directory = $FROM_DIR;
opendir (DIR, $directory) or die $!;
while (my $file = readdir(DIR))
{
next unless ($file =~ m/^[^\.]/);
my $TO_FILE = $TO_DIR.$file;
my $FROM_FILE = $FROM_DIR.$file;
#printf "To files:$TO_FILE\n";
#printf "From files:$FROM_FILE\n";
$sftp->rput($FROM_FILE ,$TO_FILE );
}
if($sftp->status)
{
print "Unsuccessfull filecopy copy to destination\n";
}
#$sftp->get("/usr/local/nagios/etc/nrpe.cfg","/usr/local/nagios/libexec/tools/linux/nrpe.cfg");
if($sftp->status)
{
print "Unsuccessfull filecopy from destination\n";
}
}
case "file"
{
my $TO_FILE = $TO_DIR.$file;
my $FROM_FILE = $FROM_DIR.$file;
$sftp->put($FROM_FILE ,$TO_FILE);
if($sftp->status)
{
print "Unsuccessfull filecopy to destination\n";
}
}
else {printf "check the option properly\n"}
}
$sftp->disconnect;
closedir(DIR);
sub print_usage {
print <
Usage: sftp_put.pl -H host -u username -p password -s "file|folder" -f "from path" -t "to path"
Options:
-H --host IPADDRESS
Check interface on the indicated host.
-u --username
Provided the user name for the remote SFTP server, which has a privilage to read/write files
-p --password
Password for the remote SFTP server
-f --from
copy file/files from the location depending on the switch used
-t --to
put the file/files in this location depending on the switch used
-s --switch
it can be either one of file ot folder
EOU
}
use Switch;
use Getopt::Long;
use Net::SFTP::Foreign;
#./sftp_put.pl -h hello.domainname.com -u root -p root123 -s
"folder" -f "/usr/local/nagios/libexec/tools/linux/" -t
"/usr/local/nagios/libexec/tools/linux/"
# ./sftp_put.pl -h hello.domainname.com -u root -p root123 -s "file" -f
"/usr/local/nagios/etc/nrpe.cfg" -t
"/usr/local/nagios/libexec/tools/linux/nrpe.cfg"
my $status = GetOptions(
"host|h=s" => \$host,
"username|u=s" => \$username,
"password|p=s" => \$password,
"from_dir|f=s" => \$FROM_DIR,
"to_dir|t=s" => \$TO_DIR,
"switch|s=s" => \$fcopy,
);
if ( $status == 0 ) {
print_usage();
exit(3);
}
my $sftp;
$sftp = Net::SFTP::Foreign->new (
$host,
timeout => 240,
user => $username,
password => $password,
autodie => 1,
);
switch ($fcopy)
{
case "folder"
{
$sftp->die_on_error("Unable to establish SFTP connection");
my $directory = $FROM_DIR;
opendir (DIR, $directory) or die $!;
while (my $file = readdir(DIR))
{
next unless ($file =~ m/^[^\.]/);
my $TO_FILE = $TO_DIR.$file;
my $FROM_FILE = $FROM_DIR.$file;
#printf "To files:$TO_FILE\n";
#printf "From files:$FROM_FILE\n";
$sftp->rput($FROM_FILE ,$TO_FILE );
}
if($sftp->status)
{
print "Unsuccessfull filecopy copy to destination\n";
}
#$sftp->get("/usr/local/nagios/etc/nrpe.cfg","/usr/local/nagios/libexec/tools/linux/nrpe.cfg");
if($sftp->status)
{
print "Unsuccessfull filecopy from destination\n";
}
}
case "file"
{
my $TO_FILE = $TO_DIR.$file;
my $FROM_FILE = $FROM_DIR.$file;
$sftp->put($FROM_FILE ,$TO_FILE);
if($sftp->status)
{
print "Unsuccessfull filecopy to destination\n";
}
}
else {printf "check the option properly\n"}
}
$sftp->disconnect;
closedir(DIR);
sub print_usage {
print <
Usage: sftp_put.pl -H host -u username -p password -s "file|folder" -f "from path" -t "to path"
Options:
-H --host IPADDRESS
Check interface on the indicated host.
-u --username
Provided the user name for the remote SFTP server, which has a privilage to read/write files
-p --password
Password for the remote SFTP server
-f --from
copy file/files from the location depending on the switch used
-t --to
put the file/files in this location depending on the switch used
-s --switch
it can be either one of file ot folder
EOU
}