Pages

Wednesday, November 9, 2022

Example - sftp file from windows

Example - sftp file from windows server

scheduled task 
 -> sftp_check.bat 
    -> sftp_check.pl 
        -> send_SFTP.bat -> send_SFTP.sh -> SEND_SFTP.ini
        -> sendSFTPMail.bat


sftp_check.bat
perl sftp_check.pl

perl sftp_check.pl
#!/usr/bin/perl

use Time::gmtime;
use File::Copy;
use Net::FTP;

############################################################################
#####A subroutine to get the current date ##################################
############################################################################
sub getDate
{
  use Time::gmtime;
  $tm=gmtime;
  ($day,$month,$year) = (gmtime) [3,4,5];
  my $locDate=sprintf("%04d%02d%02d",$tm->year+1900,($tm->mon)+1,$tm->mday);
  return $locDate;
}

############################################################################
#####A subroutine to get the current time ##################################
############################################################################
sub getTime
{
 use Time::gmtime;
 $lt=gmtime;
 ($hour,$minute,$seconds)=(gmtime) [1,2];
 my $LocTime = sprintf ("%02d%02d%02d",$lt->hour,$lt->min,$lt->sec);
 return $LocTime
}

#######################################################################################
#####A subroutine to send a file using SCP ############################################
#######################################################################################
sub SFTP_FILE
{
  local($FileNameText)=($_[0]);
  ##call the batch file
  my @SendArg=("D:\\SFTP_CHECK\\send_sftp.bat",$FileNameText);
  my $locRetCode;
  $locRetCode=system(@SendArg);
  if ($locRetCode==0)
  {
    return 1;
  }
  else
  {
    return 0;
  }
}

my $runDate=getDate;
my $runTime=getTime;

my $logFile="D:\\sftp_check\\Logs\\runLog_".$runDate."_".$runTime.".txt";
unlink $logFile;
open (MyLog,">>".$logFile);

print MyLog "### Strating sftp monitor process for date: ".$runDate." ".$runTime." ###";
print MyLog "\n";

 $retCode=SFTP_FILE;
         if ($retCode !=1 )
         {
            print MyLog "----E---- cannot SFTP ".$FileNameText." to the SFTP Server \n";
            print MyLog "\n";
            close(MyLog);
            @mailArg=("D:\\SFTP_CHECK\\sendSFTPMail.bat",$logFile);
            system(@mailArg);
            die;
         }
         else
         {
            print MyLog "File successfully sent to server.";
            print MyLog "\n";

         }

print MyLog "### End SFTP Check process ###";
print MyLog "\n";
close(MyLog); 


send_SFTP.bat
d:\software\cygwin\bin\bash.exe --login /cygdrive/D/SFTP_CHECK/send_SFTP.sh

send_SFTP.sh
sftp -b /cygdrive/d/SFTP_CHECK/SEND_SFTP.ini smart-phl@111.222.333.444

SEND_SFTP.ini
cd incoming
put /cygdrive/d/SFTP_CHECK/a.txt
bye 


sendSFTPMail.bat
blat SFTPMailBody.txt -to some.user1@domain.com, some.user2@domain.com -i some.user3@domain.com -subject "SFTP FROM BDS Failure" 

No comments:

Post a Comment