[Ssh-sftp-perl-users] Summary: using automated SFTP within a perl script
Brought to you by:
dbrobins
|
From: Ralf W. <mrs...@ya...> - 2007-07-30 15:21:40
|
Hello - First of all I like to thank Morgan Smith from this group for his h=
elp in getting the script running using SFTP. The script was tested on a RH=
EL5 server, running as a virtual machine. This script requires additional =
perl modules.=0A=0AHere is the complete listing:=0A=0A#!/usr/bin/perl -w=0A=
# This program transfers files from the host on which it is run to a series=
of=0A# hosts as specified in a configuration file, push.ctl.=0A#=0A# Autho=
r Ralf Wiegand <my...@my...>=0A# 2002/02: The script was born with=
FTP services only and limited error checking.=0A# 07/26/07 Ralf - Added SF=
TP - Secure File Transfer Protocol to the push script. This will keep the =
current=0A# data structure from the push.ctl file intact.=0A#=
07/27/07 Ralf - fixed overall structure for better readability and added p=
roper variables assignments; added use strict and use warning=0A# =
Changed to new logic and included testing via unless and eval.=0A# 07/=
30/07 Ralf - Changed if-else-logic, so if a file doesn't get transfered, it=
(the file) will not get deleted.=0A#=0A=0A# Declare perl modules=0Ause Net:=
:FTP;=0Ause Mail::Sendmail;=0Ause Net::SFTP;=0Ause Net::SSH::Perl;=0Ause st=
rict;=0Ause warnings;=0A=0A#### Declare your varibales=0Amy $timestring =3D=
localtime();=0Amy $dir =3D "/tmp";=0Amy $ibm =3D "N";=0Amy $ftp_home =3D "=
/tmp";=0Amy $hostname =3D "localhost";=0Amy $username =3D "nobody";=0Amy $p=
assword =3D "nobody";=0Amy $typ =3D "I";=0Amy $site =3D "localhost";=0Amy $=
file =3D "none";=0Amy $trans =3D "T";=0Amy $ipfile =3D "somefile";=0Amy $ft=
p =3D "ftp";=0Amy $sftp =3D "sftp";=0Amy $host =3D "localhost";=0A=0A# Prin=
t the current date & time whenever this program is called=0A$timestring =3D=
localtime();=0Aprint "----------------------------------------------------=
----\n$timestring\n";=0A=0A# The file push.ctl contains information needed =
to perform the (s)ftp for each host.=0Aopen(VARIABLES, "push.ctl"); # you n=
eed a file called push.ctl within the same directory=0A# Start main while l=
oop=0ALINE: while(<VARIABLES>) { # read in data from file and split( use :=
as divider) the data into variables=0A ($dir,$ibm,$ftp_home,$hostna=
me,$username,$password,$typ,$site,$file,$trans)=3Dsplit(/:/);=0A print "=
>>>> $dir,$ibm,$ftp_home,$hostname,$username,$password,$typ,$site,$file,$t=
rans <<<<< \n";=0A=0A ### Starting trans IF - make a decision if you w=
ant to to use SFTP or FTP ###=0A if ($trans eq "S" ) { =0A pri=
nt "Using SFTP... >> $trans << \n";=0A my $host =3D $hostname;=0A =
my %args =3D (user =3D> $username,=0A password =3D=
> $password,=0A ssh_args =3D> [port=3D>22],=0A deb=
ug =3D> 0, =0A );=0A=0A # Open the directory that hol=
ds the files to be transferred=0A if ( $dir !~ "^\/" ) { ne=
xt LINE } ;=0A chdir ($dir) or ftpWarn ("Can't cd to $dir on=
myserver;\ndirectory does not exist or permissions are wrong.") && next LI=
NE;=0A opendir(LIST,$dir);=0A=0A # If there are f=
iles to be transfered, continue.=0A if ( grep !/^\.\.?$/=
, readdir (LIST) ) {=0A rewinddir (LIST);=0A=0A =
# Connect to the sftp port on the host; cd to the directory =
to receive the files.=0A # my $sftp =3D Net::SFTP->new($host=
, %args);=0A unless( $sftp =3D eval { Net::SFTP->new($ho=
st, %args)} ) { ftpWarn("Cannot connect to $@"); }=0A =
print "Connected.\n";=0A=0A # IBM ftp servers require th=
at a site command be sent.=0A if ($ibm eq "Y" ) =
{=0A print " $ibm was activated with a Y? \n";=0A =
unless( eval { $sftp->site($site)}) { ftpWarn=
("Can't issue site command \"$site\" on $hostname;\nfiles in $dir not sent.=
") && next LINE; }=0A }=0A=0A # L=
oop through each file, excluding the files that represent the current direc=
tory (.)=0A # and parent directory (..).=0A =
FILE: foreach $ipfile ( grep !/^\.\.?$/, readdir(LIST) ) { # for loo=
p start=0A print "$dir\n";=0A =
print "$ipfile\n";=0A if ($file) { # if main st=
art=0A if( eval { $sftp->put($ipfile,$file) } ) =
{ # if A start=0A unlink($ipfile) or ftpWarn=
("Can't delete $ipfile"); # if it worked delete file if not display message=
=0A }# if A end =0A =
else {=0A ftpWarn("Can't put $ipfile on $h=
ostname;\n this could be a timeout or a permissions problem.");=0A =
}=0A }=0A else=
{=0A if( eval{ $sftp->put($ipfile,$ipfile) } ) =
{=0A unlink($ipfile) or ftpWarn("Can't delet=
e $ipfile");=0A }=0A =
else {=0A ftpWarn("Can't put $ipfile on $ho=
stname;\n this could be a timeout or a permissions problem.");=0A =
}=0A }=0A}=0A}=0A}=0A else=0A {=0A pr=
int "Using FTP.. >>> $trans <<< \n ";=0A # Open the directory that h=
olds the files to be transferred=0A if ( $dir !~ "^\/" ) { next=
LINE } ;=0A chdir ($dir) or ftpWarn ("Can't cd to $dir on m=
yserver;\ndirectory does not exist or permissions are wrong.") && next LINE=
;=0A opendir(LIST,$dir);=0A # If there are files to b=
e transfered, continue.=0A if ( grep !/^\.\.?$/, readdir (LIST) =
) {=0A rewinddir (LIST);=0A=0A # Connect to t=
he ftp port on the host; cd to the directory to receive the files.=0A =
$ftp=3DNet::FTP->new($hostname,"Timeout",600,"Debug",1) or ft=
pWarn("$@\nServer $hostname does not respond;\nfiles in $dir not sent.\nFro=
m a command line, test the connection by typing 'ftp $hostname'.") && next =
LINE;=0A $ftp->login($username, $password) or ftpWarn("I=
nvalid login on $hostname;\neither the user name or password is wrong;\nfil=
es in $dir not sent.") && next LINE;=0A $ftp->type($typ)=
or ftpWarn("Can't set type $typ on $hostname;\nfiles in $dir not sent.") &=
& next LINE;=0A if ($ftp_home) {=0A =
$ftp->cwd($ftp_home) or ftpWarn("Can't cd to $ftp_home on $hostname;=
\ndirectory does not exist or permissions are wrong;\nfiles in $dir not sen=
t.") && next LINE;=0A }=0A # IBM ftp servers =
require that a site command be sent.=0A if ($ibm eq "Y" =
) {=0A print " $ibm was activated with Y \n";=0A =
$ftp->site($site) or ftpWarn("Can't issue site command \"$site\=
" on $hostname;\nfiles in $dir not sent.") && next LINE;=0A =
}=0A # Loop through each file, excluding the files that repr=
esent the current directory (.)=0A # and parent directory (..).=
=0A=0A FILE: foreach $ipfile ( grep !/^\.\.?$/, readdir(=
LIST) ) {=0A print "$dir\n";=0A =
print "$ipfile\n";=0A if ($file) {=0A =
if( eval {$ftp->put($ipfile,$file)}) { unlink ($ipfile)=
or ftpWarn("Can't put $ipfile on $hostname;\n this could be a timeout or =
a permissions problem.");=0A }=0A =
else =0A {=0A =
if( eval { $ftp->put($ipfile)}) { unlink($ipfile) or ftpWarn("Can't d=
elete $ipfile"); } =0A else {=0A =
ftpWarn("Can't put $ipfile on $hostname;\n this could be a=
timeout or a permissions problem.");=0A =
}=0A } # closes else=0A } # =
closes id file=0A } # closees foreach loop=0A $ftp->q=
uit();=0A } # closes if-grep=0A closedir(LIST);=0A } # c=
lose trans else=0A} # close main while=0A=0A=0Asub ftpWarn {=0A=0A my ($=
message) =3D @_;=0A=0A warn "$message";=0A=0A my %mail =3D (To =
=3D> 'som...@so...',=0A From =3D> 'ftp push scri=
pt <somebody here>',=0A Subject =3D> 'FTP push error on FTP =
Server servername',=0A smtp =3D> 'servername.mydomane.com',=0A =
Message =3D> $message=0A );=0A sendmail(%mail=
) or die $Mail::Sendmail::error;=0A# print "FTP push error: $message\n";=
=0A=0A}=0A=0AI'm sure there is room for improvements, but this worked for m=
e.=0AHope it will help others.=0A=0AThank You=0ARalf Wiegand=0A=0A=0A=0A=0A=
=0A_________________________________________________________________=
___________________=0ABoardwalk for $500? In 2007? Ha! Play Monopoly Here a=
nd Now (it's updated for today's economy) at Yahoo! Games.=0Ahttp://get.gam=
es.yahoo.com/proddesc?gamekey=3Dmonopolyherenow |