sftp syntax in script | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

sftp syntax in script

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 12-18-2009
sbmk_design sbmk_design is offline
Registered User
 
Join Date: Nov 2009
Last Activity: 29 June 2010, 3:41 AM EDT
Location: India - Chennai
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
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
Sponsored Links
    #2  
Old 12-18-2009
zaxxon's Avatar
zaxxon zaxxon is offline Forum Staff  
code tag tagger
 
Join Date: Sep 2007
Last Activity: 25 November 2014, 8:27 AM EST
Location: St. Gallen, Switzerland
Posts: 6,244
Thanks: 124
Thanked 453 Times in 412 Posts
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:

http://www.unix.com/shell-programmin...ivate-key.html
Sponsored Links
    #3  
Old 12-18-2009
markdjones82 markdjones82 is offline
Registered User
 
Join Date: Dec 2009
Last Activity: 24 April 2012, 3:25 PM EDT
Location: Dallas, TX
Posts: 80
Thanks: 1
Thanked 0 Times in 0 Posts
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
sbmk_design sbmk_design is offline
Registered User
 
Join Date: Nov 2009
Last Activity: 29 June 2010, 3:41 AM EDT
Location: India - Chennai
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Hi
this sftp code works as expected.


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

Thanks
sbmk
Sponsored Links
    #5  
Old 12-18-2009
markdjones82 markdjones82 is offline
Registered User
 
Join Date: Dec 2009
Last Activity: 24 April 2012, 3:25 PM EDT
Location: Dallas, TX
Posts: 80
Thanks: 1
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by sbmk_design View Post
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?
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Syntax error in script Spartukus Shell Programming and Scripting 2 12-09-2009 09:45 AM
Syntax error with a script simpsonjr Shell Programming and Scripting 3 12-04-2009 01:54 AM
Help Syntax Shell-Script jackcracker Shell Programming and Scripting 10 08-13-2009 11:51 AM
Script syntax...does this work? 3rr0r_3rr0r UNIX for Dummies Questions & Answers 3 11-26-2007 02:39 AM
Syntax error in script KindHead Shell Programming and Scripting 2 12-12-2006 10:28 PM



All times are GMT -4. The time now is 11:56 PM.