sftp syntax in script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sftp syntax in script
# 1  
Old 12-18-2009
sftp syntax in script

I am have FTP syntax like below in my bash shell script

Code:
ftp -n <<END
        open $Server
                  user $FtpUser $FtpPwd
                  cd $FtpPath
                  binary
                  put $Filename.gz
        bye
END

Now I wanted to change this into SFTP. I tried with this... but fails
Can I have correct SFTP shell script code ?

Code:
ftp -n <<END
        open $Server
                  user $FtpUser $FtpPwd
                  cd $FtpPath
                  binary
                  put $Filename.gz
        bye
END

Thanks in advance
sbmk

Last edited by zaxxon; 12-18-2009 at 07:53 AM.. Reason: please use code tags, thank you
# 2  
Old 12-18-2009
What did you try so far on your own?
Also we just had it in another thread that it might be (if possible for your environment) better to create passwordless public keys, exchange them to your needs and do the transfer via scp which has the same syntax as rcp. Check this out:

https://www.unix.com/shell-programmin...ivate-key.html
# 3  
Old 12-18-2009
Here is the syntax for an SFTP setup that I created that reads the commands out to a temp file and then runs the SFTP using the tmp file and removes. You could always keep the batch files as well, but I didn't want to have to manage tons of them. You need Passwordless Pubkey authentication though.

I bolded the part below. I don't believe SFTP can read teh STDIN like FTP scripts, but I could be mistaken.


Code:
#!/bin/bash

#SFTP_PUSH.sh
#
#       This script is for copying files from location where
#       they are dropped to SFTP server
#
#       Author: Mark Jones
#
#       Last updated: 10/12/09

#Before implementing you can run the create_dirs script to create the archive and log directories

##### Update information below ####
CLIENT=       #client name for directory location
FEEDNAME=     #feed name for directory location
ORIGLOC=  #pickup location
DESTSERV=  #destination server name
DESTUSER=    #username for destination server
DESTLOC=/

#### End User input ####
ARCHLOC=/ftp_custom/archives/$CLIENT/$FEEDNAME
LOGFILE=/ftp_custom/logs/$CLIENT/$FEEDNAME.log
GETDATE=`date`


#       echo start date to log

echo "Started script at $GETDATE" >> $LOGFILE

# Check if items in directory then execute, else (at bottom) exit

if [ "$(find $ORIGLOC -type f)" ]; then


#       List files out to log

echo " The following file was found and is about to be transferred..." >> $LOGFILE

ls -l $ORIGLOC >> $LOGFILE

#       Copy files to archive location

cp -p $ORIGLOC/* $ARCHLOC >> $LOGFILE

#       Copy files to new location using temporary sftp batch file

cd $ORIGLOC
echo cd $DESTLOC > /tmp/$FEEDNAME.sftp
echo 'mput *' >> /tmp/$FEEDNAME.sftp
echo bye >> /tmp/$FEEDNAME.sftp
sftp -b /tmp/$FEEDNAME.sftp $DESTUSER@$DESTSERV >> $LOGFILE
if [ $? -eq "0" ]; then
        echo Files successfully transfered at $GETDATE >> $LOGFILE
        rm -f $ORIGLOC/* >> $LOGFILE
else
        echo File transfer failed at $GETDATE >> $LOGFILE
fi

rm -fv /tmp/$FEEDNAME.sftp

# Echo finish date to log
echo "Finished script at $GETDATE" >> $LOGFILE
exit 0
 # End Script if directory empty
        else
        echo "No files to process $GETDATE" >> $LOGFILE
        echo "Finished script at $GETDATE" >> $LOGFILE
        exit 0
        fi


Last edited by markdjones82; 12-18-2009 at 10:58 AM..
# 4  
Old 12-18-2009
Hi
this sftp code works as expected.


sftp $FtpUser@$Server <<END
cd $FtpPath
binary
put $Filename.gz
bye
END

Thanks
sbmk
# 5  
Old 12-18-2009
Quote:
Originally Posted by sbmk_design
Hi
this sftp code works as expected.


sftp $FtpUser@$Server <<END
cd $FtpPath
binary
put $Filename.gz
bye
END

Thanks
sbmk

sbmk, I wouldn't mind updating my script to incorperate this, but is tehre a way to send the output to log using this way?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Syntax in a simple script

I am in the process of writing a script. A very preliminary version is giving a syntax error. The script is #!/bin/bash #file1='./data/heu/hout1' exec 10<&0 exec < './data/heu/hout1' #file1='./data/heu/hout1- i=1 j=0 while read file1 do echo $file1 echo $i if then... (3 Replies)
Discussion started by: ngabrani
3 Replies

2. UNIX for Beginners Questions & Answers

Syntax error on script

Evening All (or morning for some), Could anyone have a look at the below and advise where i've going wrong with the syntax as i keep getting the below error while trying to run. Any help would be really apprecaited. ./testout: line 13: syntax error near unexpected token `else' ... (4 Replies)
Discussion started by: mutley2202
4 Replies

3. Shell Programming and Scripting

Help with syntax of Python script

I added some fields to some existing code, and all was well. Just added a few more, and BAM. I've looked this over forever, but being a newbie to Python, I see NOTHING. The code generates syslog messages, and the new fields were added around "rtp_lapse*" . Any clues??? #!/usr/bin/python ... (4 Replies)
Discussion started by: gmark99
4 Replies

4. Shell Programming and Scripting

SFTP or scp with password in a batch script without using SSH keys and expect script

Dear All, I have a requirement where I have to SFTP or SCP a file in a batch script. Unfortunately, the destination server setup is such that it doesn't allow for shell command line login. So, I am not able to set up SSH keys. My source server is having issues with Expect. So, unable to use... (5 Replies)
Discussion started by: ss112233
5 Replies

5. Shell Programming and Scripting

Script syntax help

#!/bin/bash if ; then echo "Lipsa IP"; exit; fi i=1 ip=$1 while ; do if ; then rand=`head -$i pass_file | tail -1` user=`echo $rand | awk '{print $1}'` pass=`echo $rand | awk '{print $2}'` CMD=`ps -eaf | grep -c mysql` if ; then ./mysql $ip $user $pass & else sleep 15... (6 Replies)
Discussion started by: galford
6 Replies

6. UNIX for Dummies Questions & Answers

SFTP syntax without specifying private key location (no password)

Hi all, I need to connect using SFTP from Red Hat to Windows. Connection between servers work when I specify location of my private key in the script. However, I want to use syntax without private key location specified. I know this should work, as I found it in older scripts. Scripts were... (0 Replies)
Discussion started by: yavvie
0 Replies

7. Shell Programming and Scripting

CHECK SCRIPT SYNTAX

Hi everyone, i'd like someone chechk this script, i know it's very simple but it doesn't work good, let me tell you this script works over huge directories, maybe that's the problem, if somebody can give me other way to develop, or show me where it's the problem, i'll appreciate it. ... (9 Replies)
Discussion started by: Newer
9 Replies

8. Shell Programming and Scripting

SFTP-how to log individual sftp command error while executing shell script

Hi, I have situation where i need to automate transferring 10000+ files using sftp. while read line do if ; then echo "-mput /home/student/Desktop/folder/$line/* /cygdrive/e/folder/$line/">>sftpCommand.txt fi done< files.txt sftp -b sftpCommand.txt stu@192.168.2.1 The above... (1 Reply)
Discussion started by: noobrobot
1 Replies

9. Shell Programming and Scripting

Syntax error calling TCL script from shell script

hello everyone i am beginner on shell scripting .and i am working on my project work on ad hoc network i wrote a batch (.sh) to do a looping and execute a tcl script i wrote before in each iteration ..but i got this problem " syntax error near unexpected token `('... (1 Reply)
Discussion started by: marcoss90
1 Replies

10. Shell Programming and Scripting

Syntax error in script

I get this error when I try to run my script (BTW, this is a simple script I am supposed to write for my class) $ menuscript menuscript: syntax error at line 89 : `"' unmatched $ Here is the code (Any help is greatly appreciated) (Line numbers included) 1 #!/bin/ksh 2 ... (2 Replies)
Discussion started by: KindHead
2 Replies
Login or Register to Ask a Question