shell ftp expect to intelligently copy files from

2019-08-27 05:02发布

问题:

I'm writing a shell script to automate the task of extracting data from a remote oracle database and temporarily storing it in the local machine in a folder say Csvfoder. I used 'expect' command to copy all files in this Csvfolder to the ftp server.

These csv files in the Csvfolder have specific names( In the below example timestamp has two system timestamps joined) -

For example, data extracted from oracle Table1 will have the name ABCD.(timestamp1).csv, after the script runs again after 15 mins, the Csvfolder might have another file extracted from oracle Table1 will have the name ABCD.(timestamp2).csv

Data extracted from oracle Table2 will have the name, say XYZ.(timestamp10).csv, after the script runs again after 15 mins, the Csvfolder might have another file extracted from oracle Table2 will have the name XYZ.(timestamp11).csv

Right now my script just uploads all the *.csv files in Csvfolder to the FTP server using expect.

But I have a mapping of csv file names to some specific directories on the FTP server that the csv file should go to-

From the above example, I want all ABCD.(timestampxx).csv files to be copied from my local machine to the FTP folder HOME_FOLDER/FOLDER_NAME1 (unique folder name). I want all XYZ.(timestamp).csv files to be copied from my local machine to the FTP folder HOME_FOLDER/FOLDER_NAME2 (unique folder name). In this example I will have the mapping:

ABCD files -> should go to HOME_FOLDER/FOLDER_NAME1 of FTP server
XYZ files -> should go to HOME_FOLDER/FOLDER_NAME2 of FTP server

I'm using expect with mput in my shell script right now:

expect -c '

set username harry
set ip 100.132.123.44 
set password sally
set directory /home/harry

set csvfilesFolder /Csvfolder/ 

spawn sftp $username@$ip

#Password
expect "*password:"
send "$password\n"
expect "sftp>"
#Enter the directory you want to transfer the files to
send "cd $directory\n"
expect "sftp>"
send -r "mput $csvfilesFolder*\n"


expect "sftp>"
send "exit\n"
interact'

Any suggestions as how to go about coping those csv files from local machine to specific directories on FTP server using the mapping?

回答1:

Instead of:

send -r "mput $csvfilesFolder*\n"

You want

# handle ABCD files
send "cd $directory/FOLDER_NAME1\r"
expect "sftp>"
send "mput $csvfilesFolder/ABCD.*.csv\r"
expect "sftp>"

# handle XYZ files
send "cd $directory/FOLDER_NAME2\r"
expect "sftp>"
send "mput $csvfilesFolder/XYZ.*.csv\r"
expect "sftp>"

Normally, one uses \r to simulate "hitting enter", but I guess \n works.

Use expect eof instead of interact to end the expect script.