Shell ftp希望将文件从本地智能复制到ftp服务器 [英] shell ftp expect to intelligently copy files from local to ftp server

查看:303
本文介绍了Shell ftp希望将文件从本地智能复制到ftp服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个shell脚本来自动执行从远程oracle数据库中提取数据并将其临时存储在本地计算机中Csvfoder文件夹中的任务.我使用"expect"命令将此Csvfolder中的所有文件复制到ftp服务器.

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.

Csvfolder中的这些csv文件具有特定的名称(在下面的示例中,时间戳记已加入两个系统时间戳记)-

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

例如,从oracle表1提取的数据将被命名为ABCD.(timestamp1).csv,在脚本在15分钟后再次运行之后,Csvfolder可能会从oracle表1提取另一个文件将被命名为ABCD.(timestamp2 ).csv

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

从oracle表2提取的数据将具有名称XYZ.(timestamp10).csv,该脚本在15分钟后再次运行后,Csvfolder可能会有另一个从oracle表2提取的文件将具有名称XYZ.(timestamp11). .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

现在,我的脚本只是使用Expect将Csvfolder中的所有* .csv文件上传到FTP服务器.

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

但是我将csv文件名映射到csv文件应转到的FTP服务器上的某些特定目录-

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

在上面的示例中,我希望将所有ABCD.(timestampxx).csv文件从本地计算机复制到FTP文件夹HOME_FOLDER/FOLDER_NAME1(唯一的文件夹名称). 我希望将所有XYZ.(timestamp).csv文件从本地计算机复制到FTP文件夹HOME_FOLDER/FOLDER_NAME2(唯一的文件夹名称).在此示例中,我将具有以下映射:

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

我现在在我的shell脚本中使用带有mput的Expect:

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'

关于如何使用映射将本地计算机上的csv文件处理到FTP服务器上特定目录的任何建议?

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

推荐答案

而不是:

send -r "mput $csvfilesFolder*\n"

你想要

# 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>"

通常,人们使用\r来模拟击中进入",但是我猜\n可行.

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

使用expect eof而不是interact结束期望脚本.

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

这篇关于Shell ftp希望将文件从本地智能复制到ftp服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆