Swadhin Ray
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.
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.
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@********** ~]$
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.
ASKER
I have written the script as below:
Now I am running the script like below but I am getting the errors too:
#!/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
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]$
But why you want to use sftp if it is not copying sub folders?
Did you try scp -r ?
Did you try scp -r ?
ASKER
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.
ASKER
I have changed the script which is working fine.
But want to add something like below :
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.
#!/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
But want to add something like below :
RC=$?
if [[ $RC -ne 0 ]]; then
cat output.log | "Errors Received"
else
echo "Success" | "Transfer Successful"
fi
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thanks
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