nixCraft Linux Forum

nixCraft

Linux / UNIX Tech Support Forum

Bash Shell Script To Ftp File To Server

This is a discussion on Bash Shell Script To Ftp File To Server within the Getting started tutorials forums, part of the Linux Getting Started category; Hello Nixcraft users, I had a requirement to use a shell script that I needed to call from a crontab, ...


Go Back   nixCraft Linux Forum > Linux Getting Started > Getting started tutorials

Linux answers from nixCraft.


Getting started tutorials So much to read, so little time! If that is your problem, we have solution. Read our FAQ and tutorials to help you cut through the clutter of information overload. Only members of "contributors" group can post new tutorials. Other members can just reply to thread.

Reply

 

LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 21-09-2009, 08:20 PM
jaysunn's Avatar
Powered By Linux
User
 
Join Date: Apr 2009
Location: 41.332032,-73.089775
OS: RHEL - OSX
Posts: 600
Thanks: 61
Thanked 78 Times in 70 Posts
Rep Power: 10
jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold
Default Bash Shell Script To Ftp File To Server

Hello Nixcraft users,

I had a requirement to use a shell script that I needed to call from a crontab, to contact a FTP server and put or get a file. I used the shell script below to achieve the task at hand.

You will need to modify the variable section to reflect your settings.

PHP Code:
#!/bin/bash

HOST=ftp.server.com  #This is the FTP servers host or IP address.
USER=ftpuser             #This is the FTP user that has access to the server.
PASS=password          #This is the password for the FTP user.

# Call 1. Uses the ftp command with the -inv switches.  -i turns off interactive prompting. -n Restrains FTP from attempting the auto-login feature. -v enables verbose and progress. 

ftp -inv $HOST << EOF

# Call 2. Here the login credentials are supplied by calling the variables.

user $USER $PASS

# Call 3. Here you will change to the directory where you want to put or get
cd /path/to/file

# Call4.  Here you will tell FTP to put or get the file.
put test.txt

# or
get test.txt
bye
EOF 
Now you will need to make your script executable and call it from crontab.

Code:
Prompt#chmod +x ftpscript.sh
I called this script once a day at 2:00AM daily with this crontab entry.

Code:
Prompt#crontab -e

0 2 * * * /path/to/script/ftpscript.sh

Please feel free to post any questions in regards to this setup.

Have Fun and HTH,

Jaysunn
__________________
Have a look at what I have been working on
http://www.shellasaurus.com
Reply With Quote
The Following User Says Thank You to jaysunn For This Useful Post:
anak_jkt (10-11-2009)
  #2 (permalink)  
Old 21-09-2009, 10:47 PM
nixcraft's Avatar
Never say die
User
 
Join Date: Jan 2005
Location: BIOS
OS: RHEL
Scripting language: Bash and Python
Posts: 2,709
Thanks: 11
Thanked 244 Times in 183 Posts
Rep Power: 10
nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute
Default

Yes, you can even create date folder for each day on server:
Code:
NOW=$(date +"%d-%m-%Y")
DEST="/backup/$NOW"
ftp -inv $HOST << EOF 
user $USER $PASS 
mkdir $DEST
cd $DEST
...
This may also help to other users:
FTP MySQL and Webserver System Backup script
__________________
Vivek Gite
Linux Evangelist
Be proud RHEL user, and let the world know about your enterprise choices! Join RedHat user group.
Always use CODE tags for posting system output and commands!
Do you run a Linux? Let's face it, you need help
Reply With Quote
  #3 (permalink)  
Old 21-09-2009, 11:24 PM
jaysunn's Avatar
Powered By Linux
User
 
Join Date: Apr 2009
Location: 41.332032,-73.089775
OS: RHEL - OSX
Posts: 600
Thanks: 61
Thanked 78 Times in 70 Posts
Rep Power: 10
jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold
Default

Sir,

May I say. This is a great wizard:

FTP MySQL and Webserver System Backup script

Upon viewing the php source code I was intrigued by this. As always.......

Thanks Vivek
__________________
Have a look at what I have been working on
http://www.shellasaurus.com
Reply With Quote
  #4 (permalink)  
Old 30-12-2009, 06:57 PM
Member
User
 
Join Date: May 2009
OS: Mandriva
Posts: 78
Thanks: 0
Thanked 14 Times in 14 Posts
Rep Power: 2
cfajohnson has a spectacular aura about cfajohnson has a spectacular aura about
Default

Quote:
Originally Posted by nixcraft View Post
Yes, you can even create date folder for each day on server:
Code:
NOW=$(date +"%d-%m-%Y")
DEST="/backup/$NOW"
ftp -inv $HOST << EOF 
user $USER $PASS

The username and password should be entered in ~/.netrc rather than
included in the script.
Quote:
Code:
mkdir $DEST
cd $DEST
...
This may also help to other users:
FTP MySQL and Webserver System Backup script

The ftp command is not designed for scripting.

If possible, use scp; if not, use one (or more) of the ncftp group of commands.
Reply With Quote
The Following User Says Thank You to cfajohnson For This Useful Post:
jaysunn (31-12-2009)
  #5 (permalink)  
Old 31-12-2009, 12:02 AM
jaysunn's Avatar
Powered By Linux
User
 
Join Date: Apr 2009
Location: 41.332032,-73.089775
OS: RHEL - OSX
Posts: 600
Thanks: 61
Thanked 78 Times in 70 Posts
Rep Power: 10
jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold
Default

Hello Mr. Johnson,

Quote:
The username and password should be entered in ~/.netrc rather than
included in the script.
I have searched on my BSD and RHEL servers for that file. Is this something that the admin should create to store the username and password. Also can you please explain how the script will know how to look there for the credentials. Or point me to a manual page / document.


Best Regards,

Jaysunn
__________________
Have a look at what I have been working on
http://www.shellasaurus.com
Reply With Quote
  #6 (permalink)  
Old 31-12-2009, 12:05 AM
jaysunn's Avatar
Powered By Linux
User
 
Join Date: Apr 2009
Location: 41.332032,-73.089775
OS: RHEL - OSX
Posts: 600
Thanks: 61
Thanked 78 Times in 70 Posts
Rep Power: 10
jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold
Default

Disregard.


Google is my friend.
http://linux.about.com/library/cmd/blcmdl5_netrc.htm
Unix -> .netrc
http://www.google.com/codesearch?cli...ed=0CBYQrwQwAw
Thanks for the tip.

Jaysunn
__________________
Have a look at what I have been working on
http://www.shellasaurus.com

Last edited by jaysunn; 31-12-2009 at 12:10 AM.
Reply With Quote
  #7 (permalink)  
Old 31-12-2009, 02:14 AM
Member
User
 
Join Date: May 2009
OS: Mandriva
Posts: 78
Thanks: 0
Thanked 14 Times in 14 Posts
Rep Power: 2
cfajohnson has a spectacular aura about cfajohnson has a spectacular aura about
Default

Quote:
Originally Posted by jaysunn View Post
I have searched on my BSD and RHEL servers for that file. Is this something that the admin should create to store the username and password. Also can you please explain how the script will know how to look there for the credentials. Or point me to a manual page / document.
Read the ftp man page:

Code:
man ftp
Reply With Quote
  #8 (permalink)  
Old 31-12-2009, 02:18 AM
jaysunn's Avatar
Powered By Linux
User
 
Join Date: Apr 2009
Location: 41.332032,-73.089775
OS: RHEL - OSX
Posts: 600
Thanks: 61
Thanked 78 Times in 70 Posts
Rep Power: 10
jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold jaysunn is a splendid one to behold
Default

Yes I see now.

I researched this tremendously after you have pointed it out.

RHEL:
Code:
       -n     Restrains  ftp  from  attempting  ''auto-login'' upon initial connection.  If auto-login is enabled, ftp will check the .netrc (see below) file in the user's home directory for an
              entry describing an account on the remote machine.  If no entry exists, ftp will prompt for the remote machine login name (default is the user identity on the local machine), and,
              if necessary, prompt for a password and an account with which to login.
OSX 10.6.2
Code:
 -N netrc    Use netrc instead of ~/.netrc.  Refer to THE .netrc FILE for more information.

Thanks for the insight. I am excited to implement this on many servers. As well as getting my senior engineer jealous for understanding this before him....


THanks Again,

Jaysunn
__________________
Have a look at what I have been working on
http://www.shellasaurus.com
Reply With Quote
  #9 (permalink)  
Old 01-01-2010, 11:24 AM
rockdalinux's Avatar
Is that all you got?
User
 
Join Date: May 2005
Location: Planet Vegeta
OS: Redhat
Posts: 705
Thanks: 15
Thanked 19 Times in 18 Posts
Rep Power: 10
rockdalinux is a glorious beacon of light rockdalinux is a glorious beacon of light rockdalinux is a glorious beacon of light rockdalinux is a glorious beacon of light rockdalinux is a glorious beacon of light rockdalinux is a glorious beacon of light
Default

.netrc is standard these days, but if you ever get chance to work on older proprietary UNIX os like *cough* Solairs, AIX *cough* you will not find anything like .netrc. This is main reason for using ftp -u user syntax. Also ~/.netrc does not make your script secure. Both has its own security problem. If possible avoid using ftp and switch to rsync or scp / sftp. Those are secure and much better alternatives. These days OpenSSH is part of all UNIX like operating systems except *cough* MS-Windows server *cough*
__________________
Rocky Jr.
What's wrong? I hope I am not making you uncomfortable...

Never send a boy to do a mans job.
Reply With Quote
Reply

Tags
ftp , ftp login , ftp script , ftp script linux , ftp script unix , ftp shell script


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 
Thread Tools
Display Modes

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 On
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads

Thread Thread Starter Forum Replies Last Post
[Solved] UNIX File Descriptors In Bash Shell raj Shell scripting 5 28-09-2009 03:57 AM
Bash FTP unload script (single file from list) Boruts5 Shell scripting 0 26-02-2009 03:51 PM
Please suggest me this easy bash shell script nextcmchiranjeevi Shell scripting 1 11-02-2009 02:34 AM
Shell Script To Outputs File Permissions of Most Recently Modified File glen_4455 Shell scripting 1 25-08-2008 02:39 PM
shell script to search specific file from txt file inside zip file and extract it aasif.shaikh Shell scripting 2 31-05-2008 06:44 PM


All times are GMT +5.5. The time now is 11:37 AM.


Powered by vBulletin® Version 3.8.5 - Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2
©2005-2010 nixCraft. All rights reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38