KVi
KVi

Reputation: 3

BASH: Send directory files from server(non-sftp) to remote server that use sftp

I can send files one by one by changing the file name, the files are increasing in number so I tried using for loop but it did not work.

I need to send all the files in FILE variable to the remote machine USER@$HOST:/Semi/KFILE-ENG/ELD. Also, the remote machine is sftp enabled, while the sender is using ftp only. This is what I only have.

HOST=ftpabc.abc.cn
PORT=22
USER=user
PASSWORD=pass
FILE=/u01/flatringDev/ToVendor/LC7/ELD/ELD.7

spawn /usr/bin/sftp $USER@$HOST:/Semi/KFILE-ENG
expect "password:"
send "$PASSWORD\r"
expect "sftp>"
send "mkdir ELD\r"
expect "sftp>"
send "cd ELD\r"
expect "sftp>"
send "put $FILE\r"
expect "sftp>"
send "bye\r"

If someone knows a solution, please let me know. Thank you

Upvotes: 0

Views: 359

Answers (1)

PradyJord
PradyJord

Reputation: 2160

#!/bin/bash

HOST=ftpabc.abc.cn
PORT=22
USER=user
PASSWORD=pass
declare -a arry
arry=('file1' 'file2' 'file3' 'file4' '/tmp/kl.pl') # list files with absolute path eg: /tmp/kl.pl
#for FILE in $(ls -1 DIR_containing_files) # If you can all files in same dir, else create an array with elements as filenames with absoulte path and use 2nd for statement

for FILE in ${arry[*]} 
do

spawn /usr/bin/sftp $USER@$HOST:/Semi/KFILE-ENG
expect "password:"
send "$PASSWORD\r"
expect "sftp>"
send "mkdir ELD\r"
expect "sftp>"
send "cd ELD\r"
expect "sftp>"
send "put $FILE\r"
expect "sftp>"
send "bye\r"

sleep 5

done

Check if this works, you can have a bash function doing all the expect thing and call it in for loop or an expect script file and call that in bash for

Upvotes: 0

Related Questions