Sponsored Content
Full Discussion: sftp syntax in script
Top Forums Shell Programming and Scripting sftp syntax in script Post 302381465 by markdjones82 on Friday 18th of December 2009 09:49:00 AM
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..
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
SFTP-SERVER(8)						    BSD System Manager's Manual 					    SFTP-SERVER(8)

NAME
sftp-server -- SFTP server subsystem SYNOPSIS
sftp-server [-ehR] [-f log_facility] [-l log_level] [-u umask] DESCRIPTION
sftp-server is a program that speaks the server side of SFTP protocol to stdout and expects client requests from stdin. sftp-server is not intended to be called directly, but from sshd(8) using the Subsystem option. Command-line flags to sftp-server should be specified in the Subsystem declaration. See sshd_config(5) for more information. Valid options are: -e Causes sftp-server to print logging information to stderr instead of syslog for debugging. -f log_facility Specifies the facility code that is used when logging messages from sftp-server. The possible values are: DAEMON, USER, AUTH, LOCAL0, LOCAL1, LOCAL2, LOCAL3, LOCAL4, LOCAL5, LOCAL6, LOCAL7. The default is AUTH. -h Displays sftp-server usage information. -l log_level Specifies which messages will be logged by sftp-server. The possible values are: QUIET, FATAL, ERROR, INFO, VERBOSE, DEBUG, DEBUG1, DEBUG2, and DEBUG3. INFO and VERBOSE log transactions that sftp-server performs on behalf of the client. DEBUG and DEBUG1 are equivalent. DEBUG2 and DEBUG3 each specify higher levels of debugging output. The default is ERROR. -R Places this instance of sftp-server into a read-only mode. Attempts to open files for writing, as well as other operations that change the state of the filesystem, will be denied. -u umask Sets an explicit umask(2) to be applied to newly-created files and directories, instead of the user's default mask. For logging to work, sftp-server must be able to access /dev/log. Use of sftp-server in a chroot configuration therefore requires that syslogd(8) establish a logging socket inside the chroot directory. SEE ALSO
sftp(1), ssh(1), sshd_config(5), sshd(8) T. Ylonen and S. Lehtinen, SSH File Transfer Protocol, draft-ietf-secsh-filexfer-00.txt, January 2001, work in progress material. HISTORY
sftp-server first appeared in OpenBSD 2.8. AUTHORS
Markus Friedl <markus@openbsd.org> BSD
January 9, 2010 BSD
All times are GMT -4. The time now is 03:32 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy