From 162fb44dd7140925449b0fedb1ce126499accf0d Mon Sep 17 00:00:00 2001 From: "ismael.moreno" Date: Tue, 10 Sep 2019 14:26:50 +0200 Subject: [PATCH] SFTP support and custom port added --- pandora_plugins/FTP/ftp_plugin/ftp.conf | 22 ++++++---- pandora_plugins/FTP/ftp_plugin/plugin_ftp.pl | 43 ++++++++++++++++---- 2 files changed, 49 insertions(+), 16 deletions(-) diff --git a/pandora_plugins/FTP/ftp_plugin/ftp.conf b/pandora_plugins/FTP/ftp_plugin/ftp.conf index ff418cdcfb..2d0099b3be 100644 --- a/pandora_plugins/FTP/ftp_plugin/ftp.conf +++ b/pandora_plugins/FTP/ftp_plugin/ftp.conf @@ -5,25 +5,31 @@ #====================================================================== # User and password for FTP connection -conf_ftp_user mario +conf_ftp_user root # Use "" if your password is in blank -conf_ftp_pass pulido +conf_ftp_pass Password + +#Port for FTP/SFTP connection +#conf_ftp_port 22 + +#Set this parameter to 1 if you want stablish an SFTP connection +conf_ftp_sftp 1 # Configure complete name of ftp file --> upload (Local) -conf_ftp_putfile /home/mariopc/Descargas/ejemplo.zip +conf_ftp_putfile /tmp/test_upload.zip # Configure name of ftp file --> upload (FTP server) -conf_ftp_putname prueba.zip +conf_ftp_putname /tmp/test.zip # Configure Ip for FTP Connection conf_ftp_host localhost # Configure name of ftp file --> download (FTP server) -conf_ftp_getfile prueba.zip +conf_ftp_getfile /tmp/test.zip # Configure complete name os ftp file --> download (Local) -conf_ftp_getname prueba.zip +conf_ftp_getname /tmp/test.zip # Configure Operating System (Unix or Windows) conf_operating_system Unix @@ -34,10 +40,10 @@ conf_operating_system Unix conf_ftp_compare write -conf_ftp_compare_file prueba.zip +conf_ftp_compare_file /tmp/prueba.zip -conf_local_comp_file prueba.zip +conf_local_comp_file /tmp/prueba.zip conf_local_downcomp_file /tmp/prueba.zip diff --git a/pandora_plugins/FTP/ftp_plugin/plugin_ftp.pl b/pandora_plugins/FTP/ftp_plugin/plugin_ftp.pl index 0aa463e597..e464c42057 100644 --- a/pandora_plugins/FTP/ftp_plugin/plugin_ftp.pl +++ b/pandora_plugins/FTP/ftp_plugin/plugin_ftp.pl @@ -8,6 +8,7 @@ use strict; use warnings; use Data::Dumper; use Net::FTP; +use Net::SFTP::Foreign; use Time::HiRes qw ( gettimeofday ); my $archivo_cfg = $ARGV[0]; @@ -115,6 +116,14 @@ sub load_external_setup ($) if ($parametro =~ m/^conf\_ftp\_host\s(.*)/i) { $plugin_setup{"conf_ftp_host"} = $1; } + + if ($parametro =~ m/^conf\_ftp\_port\s(.*)/i) { + $plugin_setup{"conf_ftp_port"} = $1; + } + + if ($parametro =~ m/^conf\_ftp\_sftp\s(.*)/i) { + $plugin_setup{"conf_ftp_sftp"} = $1; + } if ($parametro =~ m/^conf\_ftp\_putfile\s(.*)/i) { $plugin_setup{"conf_ftp_putfile"} = $1; @@ -184,20 +193,38 @@ load_external_setup ($archivo_cfg); #------------------------------------------------------------------------- # Start session in FTP server #-------------------------------------------------------------------------- - -my $ftp = Net::FTP->new($plugin_setup{"conf_ftp_host"}) or die("Unable to connect to server: $!");#Connect FTP server -$ftp->login($plugin_setup{"conf_ftp_user"},$plugin_setup{"conf_ftp_pass"}) or die("Failed Login: $!");# Login at FTP server +my $ftp; +if (($plugin_setup{"conf_ftp_sftp"}) && ( $plugin_setup{"conf_ftp_sftp"} == 1)){ + if ($plugin_setup{"conf_ftp_port"}){ + #port => $plugin_setup{"port"}, + $ftp = Net::SFTP::Foreign->new(host => $plugin_setup{"conf_ftp_host"}, port => $plugin_setup{"conf_ftp_port"}, stderr_discard => 1, user => $plugin_setup{"conf_ftp_user"} , password => $plugin_setup{"conf_ftp_pass"},expect_log_user => 'false'); + if($ftp->error){ + die($ftp->error); + } + }else{ + $ftp = Net::SFTP::Foreign->new(host => $plugin_setup{"conf_ftp_host"}, stderr_discard => 1, user => $plugin_setup{"conf_ftp_user"} , password => $plugin_setup{"conf_ftp_pass"},expect_log_user => 'false'); + if($ftp->error){ + die($ftp->error); + } + } +} else { + if ($plugin_setup{"conf_ftp_port"}){ + $ftp = Net::FTP->new(host => $plugin_setup{"conf_ftp_host"},port => $plugin_setup{"conf_ftp_port"}) or die("Unable to connect to server: $!");#Connect FTP server + }else{ + $ftp = Net::FTP->new($plugin_setup{"conf_ftp_host"}) or die("Unable to connect to server: $!");#Connect FTP server + } + $ftp->login($plugin_setup{"conf_ftp_user"},$plugin_setup{"conf_ftp_pass"}) or die("Failed Login: $!");# Login at FTP server #print_module ( "Disp_FTP_$plugin_setup{conf_ftp_host}" , "generic_proc", 1, " Determines whether FTP login to $plugin_setup{conf_ftp_host} has been successful or not" ); +} #------------------------------------------------------------------------- # Returns the module that shows the time and transfer rate.(Upload a file) #-------------------------------------------------------------------------- my $clock0 = gettimeofday(); - $ftp->put($plugin_setup{"conf_ftp_putfile"},$plugin_setup{"conf_ftp_putname"});# Upload file at FTP server + $ftp->put($plugin_setup{"conf_ftp_putfile"},$plugin_setup{"conf_ftp_putname"}) or die("Cannot upload file to server");# Upload file at FTP server my $clock1 = gettimeofday(); my $clockd = $clock1 - $clock0;# Calculate upload transfer time - $ftp->size($plugin_setup{"conf_ftp_putname"});# File size - my $putrate = $ftp->size($plugin_setup{"conf_ftp_putname"})/$clockd;# Calculate rate transfer + my $putrate = $ftp->stat($plugin_setup{"conf_ftp_putname"})->size/$clockd;# Calculate rate transfer my $time_puftp=sprintf("%.2f",$clockd); my $rate_puftp=sprintf("%.2f",$putrate); @@ -212,8 +239,8 @@ $ftp->login($plugin_setup{"conf_ftp_user"},$plugin_setup{"conf_ftp_pass"}) or di $ftp->get($plugin_setup{"conf_ftp_getfile"},$plugin_setup{"conf_ftp_getname"}); my $clock3 = gettimeofday(); my $clockg = $clock3 - $clock2; - $ftp->size($plugin_setup{"conf_ftp_getname"}); - my $getrate = $ftp->size($plugin_setup{"conf_ftp_getname"})/$clockg; + #$ftp->stat($plugin_setup{"conf_ftp_getname"})->size; + my $getrate = $ftp->stat($plugin_setup{"conf_ftp_getname"})->size/$clockg; my $time_getftp=sprintf("%.2f",$clockg); my $rate_getftp=sprintf("%.2f",$getrate);