SFTP using BASH script

Hello Experts,

I want to write a script to send folder from local server to another server using SFTP.

Using username "oracle".
oracle@**********'s password:
Last login: Wed Aug 19 23:15:50 2015 from ***********
[oracle@********** ~]$ sftp root@**********
Connecting to **********...
root@**********'s password:
sftp> cd /root/Desktop/exp_files
sftp> lcd /home/oracle/app/EXP_DIR
sftp> put test.txt
Uploading test.txt to /root/Desktop/exp_files/test.txt
test.txt                                      100%    5     0.0KB/s   00:00
sftp> put test
skipping non-regular file test
sftp> put test/
skipping non-regular file test/
sftp> put -r test
put: Invalid flag -r
sftp> put -r test/*
put: Invalid flag -r
sftp> bye
[oracle@********** ~]$

Open in new window


I know if I zip the folder then I can send it and unzip it but this is not allowed as per the customer.
So can I create the same directory on the SFTP server and move all the files under it .

The script what I was looking to take 4 parameters like : host name , username , password , local path , remote path , foldername

Looking for the help here ,

Thanks in advance.
LVL 17
Swadhin RaySenior Technical Engineer Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

omarfaridCommented:
you can use scp -r rather sftp. To automate passing user name/password you can use expect. Or you can set trust between your account and remote account (in this case password is required).

Please see links below for more info / help.

http://expect.sourceforge.net/
https://blogs.oracle.com/jkini/entry/how_to_scp_scp_and
http://linux.die.net/man/1/scp
http://www.hypexr.org/linux_scp_help.php
0
Swadhin RaySenior Technical Engineer Author Commented:
I have written the script as below:

#!/bin/bash
 HOST=$1
 USER=$2
 PASSWORD=$3
 SOURCE_DIR=$4
 SOURCE_FILE=$5
 TARGET_DIR=$6
 
/usr/bin/expect<<EOD > output.log
spawn /usr/bin/sftp -o $USER@$HOST
expect "password:"
send "$PASSWORD\r"
expect "sftp>"
send "lcd $SOURCE_DIR\r"
expect "sftp>"
send "cd $TARGET_DIR\r"
expect "sftp>"
send "put $SOURCE_FILE\r"
expect "sftp>"
send "bye\r"
EOD
RC=$?
if [[ ${RC} -ne 0 ]]; then
  cat output.log | "Errors Received" 
else
  echo "Success" | "Transfer Successful" 
fi

Open in new window


Now I am running the script like below but I am getting the errors too:

oracle@vmidelpgeismdb sftp]$ ./sftp.sh myhost root metlan /home/oracle/app/EXP_DIR test.txt /root/Desktop/exp_files
invalid command name "sftp"
    while executing
"sftp -o root@myhost"
./sftp.sh: line 24: Errors Received: command not found
[oracle@vmidelpgeismdb sftp]$ ./sftp.sh myhost root metlan /home/oracle/app/EXP_DIR test.txt /root/Desktop/exp_files
send: spawn id exp4 not open
    while executing
"send "metlan\r""
./sftp.sh: line 24: Errors Received: command not found
[oracle@vmidelpgeismdb sftp]$

Open in new window

0
omarfaridCommented:
But why you want to use sftp if it is not copying sub folders?

Did you try scp -r ?
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Swadhin RaySenior Technical Engineer Author Commented:
will create a folder on target machine and then will place all the files from source to target. but yes want to use only SFTP.
0
Swadhin RaySenior Technical Engineer Author Commented:
I have changed the script which is working fine.

#!/usr/bin/expect

set timeout -1

if { $argc != 6 } {
    puts "Usage $argv0 host user pass cd lcd file "
    exit 1
}

set host [lindex $argv 0]
set user [lindex $argv 1]
set pass [lindex $argv 2]
set cd [lindex $argv 3]
set lcd [lindex $argv 4]
set file [lindex $argv 5]

spawn sftp -oStrictHostKeyChecking=no -oCheckHostIP=no $user@$host
expect *assword:

send "$pass\r"
expect sftp>

send "cd $cd\r"
expect sftp>

send "lcd $lcd\r"
expect sftp>
send "put $file\r"

send "exit\r"
expect eof

Open in new window


But want to add something like below :

RC=$?
if [[ $RC -ne 0 ]]; then
  cat output.log | "Errors Received" 
else
  echo "Success" | "Transfer Successful" 
fi

Open in new window


So that when i execute the script this will return me if file transfer is success or failed .

Can you help me in this part.
0
omarfaridCommented:
I am not sure how to do that, but you can use expect to handle returned messages after every command you send to sftp.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Swadhin RaySenior Technical Engineer Author Commented:
thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Shell Scripting

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.