Link to home
Start Free TrialLog in
Avatar of Swadhin Ray
Swadhin RayFlag for United States of America

asked on

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.
Avatar of omarfarid
omarfarid
Flag of United Arab Emirates image

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
Avatar of Swadhin Ray

ASKER

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

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

Did you try scp -r ?
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.
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.
ASKER CERTIFIED SOLUTION
Avatar of omarfarid
omarfarid
Flag of United Arab Emirates image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thanks