Shell script for multiple files uploading using SFTP. (4227 Views)
Reply
Regular Advisor
Posts: 237
Registered: ‎12-30-2007
Message 1 of 3 (4,227 Views)

Shell script for multiple files uploading using SFTP.

Hi,

We have the shell script for multiple files uploading using SFTP as below to uploads all the files (mput *) on customer SFTP server.

 

But we have requirement that customer wants all the files to come on there sftp server as same file name i.e. abcde. As on there side all the files which we will be uploading will get appended to there single file i.e. abcde.

 

I don;t know how to put the logic basically it should read all the files available in directory i.e. /tmp/files and put each single file on customer SFTP server as (put file1 abcde) (put file2 abcde) (put file3 abcde) and so on.

 

Please let me know how to modify the below script to meet the customer requirement. We have 3 script as below to upload multiple files.

 

cat Test_ACH
#!/bin/ksh
echo "**********************************************"  > /test/log/sftp_Test_ACH.log
echo "****   Test ACH  ****"  >> /test/log/sftp_Test_ACH.log
echo "**********************************************"  >> /test/log/sftp_Test_ACH.log
chmod 666 /test/log/sftp_Test_ACH.log

Datestamp=$(date +%Y%m%d%H%M)
su - sftptest -c "/test/script/Test_ACH_sftp"  >> /test/log/sftp_Test_ACH.log

cp -p /test/log/sftp_Test_ACH.log /test/log/sftp_Test_ACH.log.$Datestamp
/usr/lib/sendmail narendra.uttekar@xyz.com < /test/log/sftp_Test_ACH.log.$Datestamp

------------------------------------------------------------------------------------------------------------------------------

 

cat Test_ACH_sftp
#!/bin/ksh
cd /tmp/files

file=$(ls)
/usr/bin/sftp -b /test/script/ftpcmd_Test nuttekar@test2
RC=$?
echo " "
echo "sftp return code is : $RC"
if test $RC -eq 255
then
     echo "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
     echo "There is some problem connecting to the ftp server so exiting the script here"
     echo "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
     exit
elif test $RC -eq 1
then
     echo "Looks like there are no files to transfer to the ftp server"
elif test $RC -eq 0
then
     echo "ACH sftp ran OK"
else
     echo "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
     echo "There was some unknown error occurred during sftp so exiting the script here"
     echo "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
     exit
fi

Datestamp=$(date +%Y%m%d%H%M)

file=$(ls)
N=0
for x in $file
do
    (( N = N + 1 ))
    echo "Processing the file: $x"
    mv $x ../backup/$x.$Datestamp
done
echo "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
echo "Number of files transferred to the ftp server are: $N"
echo "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"

----------------------------------------------------------------------------------------------------------------------------------

 

cat ftpcmd_Test

cd /home/nuttekar/test
mput *
quit

 

Thanks,

Narendra

Honored Contributor
Posts: 13,832
Registered: ‎06-21-2000
Message 2 of 3 (4,208 Views)

Re: Shell script for multiple files uploading using SFTP.

As far as I know SFTP does not have the capability to append one file to the end of another file.

 

If you invoke SFTP and type 'help' you should see a list of commands available.

 

Normal FTP has the 'append' option.  SFTP does not.

 

SFTP commands from my server:

 

sftp> help
Available commands:
cd path                       Change remote directory to 'path'
lcd path                      Change local directory to 'path'
chgrp grp path                Change group of file 'path' to 'grp'
chmod mode path               Change permissions of file 'path' to 'mode'
chown own path                Change owner of file 'path' to 'own'
help                          Display this help text
get remote-path [local-path]  Download file
lls [ls-options [path]]       Display local directory listing
ln oldpath newpath            Symlink remote file
lmkdir path                   Create local directory
lpwd                          Print local working directory
ls [path]                     Display remote directory listing
lumask umask                  Set local umask to 'umask'
mkdir path                    Create remote directory
progress                      Toggle display of progress meter
put local-path [remote-path]  Upload file
pwd                           Display remote working directory
exit                          Quit sftp
quit                          Quit sftp
rename oldpath newpath        Rename remote file
rmdir path                    Remove remote directory
rm path                       Delete remote file
symlink oldpath newpath       Symlink remote file
version                       Show SFTP version
!command                      Execute 'command' in local shell
!                             Escape to local shell
?                             Synonym for help

 

 

 

Acclaimed Contributor
Posts: 25,604
Registered: ‎03-06-2006
Message 3 of 3 (4,197 Views)

Re: Shell script for multiple files uploading using sftp

>/usr/lib/sendmail narendra.uttekar@xyz.com < /test/log/sftp_Test_ACH.log.$Datestamp

 

It's easier to use mailx to send mail.

 

>if test $RC -eq 255

 

Why not: if [ $RC -eq 255 ]; then

 

>cd /home/nuttekar/test
>mput *

 

This mput won't even append, there will be N separate files there.  And if you don't turn off prompts (and like ftp),

you'll get separate queries for each.

 

>As far as I know SFTP does not have the capability to append one file to the end of another file.

 

Oh boy.

I suppose you could get the file.  Then append to it.  Then copy it  back with another name.

Then rename the old, rename the new, then remove the old.

 

But if there are multiple scripts trying to copy files there, there will be problems.  Or if they get killed in the middle.

The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.