Go Back   The macosxhints Forums > OS X Help Requests > UNIX - General



Reply
 
Thread Tools Rating: Thread Rating: 3 votes, 5.00 average. Display Modes
Old 07-06-2011, 08:12 PM   #1
imonkey
Prospect
 
Join Date: Jun 2010
Posts: 14
Scripting SFTP [non-interactive]

Hi All,
I'm trying to automate a file upload via sftp. My though was to write a simple shell script and run that script via launchd. Looking at the man page for sftp it appears that the sftp command only works in interactive mode. which would confirm why my script is not working:

Code:
#!/bin/bash
# Uploads a specified file to a remote server via sftp

# Sets the remote Server
SERVER="user@sftp.server.com.au"

# Sets the local file to upload
FILE="/path/to/file"

# Upload the file to the server
SFTP $SERVER
put $FILE
quit

exit 0
When I run it is stops at the prompt sftp> then when I manually quit it attempts the next two lines not knowing the put and quit commands.

The sftp server I'm trying to connect to is a Windows box running WinSSHD http://www.bitvise.com/winsshd. I tried doing a scp copy via ssh but the terminal session doesn't start—I'm presuming an incompatibility with the window command line

I've setup private/public keys on the server and user account so no password is required.

Any suggestions on automating a file copy via ssh or sftp to a windows server? I'm thinking of just using Panic's Transmit disk feature and doing a copy that way.


Thanks in advance.
imonkey is offline   Reply With Quote
Old 07-06-2011, 08:26 PM   #2
hayne
Site Admin
 
Join Date: Jan 2002
Location: Montreal
Posts: 32,389
Perhaps using the 'expect' command will help.
('expect' is designed for scripting interactive command-line programs)
Read 'man expect'
__________________
hayne.net/macosx.html
hayne is offline   Reply With Quote
Old 07-07-2011, 12:20 AM   #3
SirDice
MVP
 
Join Date: Aug 2009
Posts: 1,119
Why don't you use scp?
Code:
#!/bin/bash
# Uploads a specified file to a remote server via sftp

# Sets the remote Server
SERVER="user@sftp.server.com.au"

# Sets the local file to upload
FILE="/path/to/file"

# Upload the file to the server
scp $FILE $SERVER

exit 0
Edit: scratch that, it doesn't seem to work for you.

Another option is to use the batch mode of sftp:
Code:
(echo put $FILE; echo quit) | sftp -b - $SERVER

exit 0

Last edited by SirDice; 07-07-2011 at 12:27 AM.
SirDice is offline   Reply With Quote
Old 07-08-2011, 12:59 AM   #4
imonkey
Prospect
 
Join Date: Jun 2010
Posts: 14
That worked a treat, Thanks SirDice
imonkey is offline   Reply With Quote
Reply

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump



All times are GMT -5. The time now is 02:23 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.