Script to FTP invisibly

Hey guys. I'm not sure what kind of script would be best for this. I need to schedule the upload of a file via FTP and replace the original on the server, without any apps or windows opening on my mac. Is this possible with a shell command? I'm not really familiar with OS scripts as I'm a web developer. I basically need to do this every 5 minutes:


Connect to: ftp.example.com/test/

Username: rich

Password: xxx

Upload from: ~rich/documents/test/

File to upload: image.png

replace: yes

Disconnect


Any help would be awesome as I'm having to check/replace this file manually every hour at the moment - long story! Thanks 🙂

Posted on Jun 28, 2014 6:30 AM

Reply
5 replies

Jun 28, 2014 8:19 AM in response to richmlee

Assuming you would prefer using a secure protocol (ssh and its associated sftp and scp tools) and not making your login credentials visible for anyone with access to the network (such as the folks running the coffee shop or hotel WiFi), and assuming you would prefer to use a protocol that is actually designed for and compatible with modern networks — and not a protocol (ftp) that is totally and irrevocably insecure, massively archaic (ftp entirely predates the IP-based Internet), and fundamentally incompatible with modern network security, you can use the following command:


scp -B sourcefile rich@ftp.example.com:/path/to/targetfile


That sequence does require setting up certificates, but it means you don't have to expose your password, either.


scp (one-shot file copy) and sftp (same general interface as ftp) are based on ssh, and use the same certificates and related details — once you get ssh certificates set up and the logins working, then sftp and scp will also operate.


I don't have a general write-up of this sequence, but using PuTTY (on Windows) and ssh on OS X (and when ssh is working, so is scp) covers most of this, and some related details (that latter article covers a particular target server operating system, you'd need to use the sequence for whatever target server you're working with here. Here's a bash shell script that I use to make remote MySQL database archives — this script performs a nightly transfer using AFP for the network transfer connection and not ftp nor sftp/scp tools; you'd need to use the hourly part of periodic, and rewrite the script to meet your particular needs.


There are probably commercial tools around which can solve this problem for you, as well. (I've not looked for any, though.)


If you really want to use the insecure morass that is ftp, then start reading here for details of how to expose your login credentials.

Jun 28, 2014 11:43 AM in response to MrHoffman

Thanks for all the info. Yes FTP is fine as this will be done from a secure machine. I'm using the following script in Terminal, which works fine.


cd documents/test;curl -v -T image.png ftp://rich:xxx@www.example.com/public_html/test/;exit


Is this classed as a shell script and do you know how I could schedule/run it without opening terminal?


Thanks again

Jun 28, 2014 12:28 PM in response to richmlee

See the periodic script that was linked for how to run stuff periodically (or look around for details on using launchctl and launchd), and see the stackoverflow article that was linked for how to script ftp using the expect tool. Scripting ftp tends to involve the expect command to enter the password. Unlike doing things (securely) with scp and certificates, using ftp here is not at all straightforward. But the stackoverflow article has an example of how to do it.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Script to FTP invisibly

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.