LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-06-2006, 11:30 AM   #1
ksakamuri
LQ Newbie
 
Registered: May 2006
Posts: 4

Rep: Reputation: 0
How to automate an FTP process?


Hello Linux experts,

I am newbie to Linux and shell script. But I have to write a shell script (ASAP) where I need to ftp a file on daily basis to a remote server, and send an email with final status. I I should have a properties file with hostname, Userid, and pwd. And a shall script file should read the values from that property file and check the file (abc.txt) availability in the local server and ftp the file. Log the error to abc.log file Send an email with final status

Can any body help me with a sample code or a link where I can find some examples?

Thanks in advance for your help.

KK
 
Old 10-06-2006, 06:25 PM   #2
stress_junkie
Senior Member
 
Registered: Dec 2005
Location: Massachusetts, USA
Distribution: Ubuntu 10.04 and CentOS 5.5
Posts: 3,873

Rep: Reputation: 332Reputation: 332Reputation: 332Reputation: 332
You should consider using passwordless ssh connections. This would encrypt the data while it is being transmitted and it would mean that you don't have to put a password into a configuration file. It is considered a bad idea to put passwords into configuration files or into scripts. If you used ssh connections then you could either use scp or sftp to copy the files. If you don't use ssh then you still have the choice between using rcp or ftp to copy the files. If the destination computer is on your LAN then scp or rcp might be easier to configure than ftp or sftp. Nevertheless this is how I would do the job as you described it.

Don't use the root account to do the copying. You should have a normal user account to do the copying. Naturally this account will have to have read access to the file to copy. Let's call this account abcftp and it will have a home directory in /home/abcftp. The abc.txt file will reside in /home/abcftp. For simplicity we will call the script abcftp.sh and it will also reside in /home/abcftp. Also, the user name on the remote computer is abcftp. I'm not able to test this so there may be some debugging required.
The user account should have a .bashrc in its home directory. That .bashrc file should contain the following line.
Code:
export MAILTO=root@localhost
This will determine where the cron daemon will send the log file of the script.

The abcftp.sh file should contain this:
Code:
#! /bin/bash
# This file will use ftp to copy the file /home/abcftp/abc.txt to a remote
# server. It uses the file /home/abcftp/.netrc for remote server
# authentication credentials.
if [[ -e /home/abcftp/abc.txt ]] ; then
 ftp -v abcftp@remote.computer 
    send abc.txt
    quit
 exit;
else
 echo The abc.txt file was not available at `date +%F %H:%M`;
fi
exit
Once you have created this file then you have to change its permissions to allow it to execute. Use the chmod command as follows:
Code:
chmod u+x abcftp.sh
Then you need to create a file called .netrc. It should contain the following lines.
Code:
machine remote.computer
login abcftp
password <password>
The <password> is the real password.

The .netrc file has to be readable by ONLY the abcftp account or else the ftp software will not use it. You use the chmod command to accomplish this.
Code:
chmod u=rw,g-rwx,o-rwx .netrc
You should be able to test this by calling the script as follows.
Code:
/home/abcftp/abcftp.sh
Make sure that is working before you bother doing any of the following.

Next you want to get this to automatically run at a given time. You do this with the cron service. You will create your own crontab file with a line to tell the cron daemon when to run this script. You create a new crontab file or edit an existing one using the crontab command.
Code:
crontab -e
The syntax of the crontab records is a little bit confusing because you can have several types of qualifiers regarding the days and times to execute a given command. You can read about the syntax of the crontab file by entering the following command.
Code:
man 5 crontab
Let's say that you want the job to run Monday through Friday at 8:00 p.m. In twenty four hour format the time is 20:00. The crontab entry should look like this.
Code:
* 20 * 1-5 /home/abcftp/abcftp.sh > /home/abcftp/abcftp.log 2>&1
The crontab default editor is vi.

Let us know if you succeeded or if you need help.

Last edited by stress_junkie; 10-06-2006 at 06:31 PM.
 
Old 10-06-2006, 06:42 PM   #3
haertig
Senior Member
 
Registered: Nov 2004
Distribution: Debian, Ubuntu, LinuxMint, Slackware, SysrescueCD
Posts: 2,114

Rep: Reputation: 330Reputation: 330Reputation: 330Reputation: 330
If this is a homework assignment (your description makes it sound like one) you shouldn't be asking for examples here. (1) You don't learn anything, and (2) It's against the rules of the forum.

If it's not homework, you're probably free to come up with a better solution. stress_junkie already pointed out ssh/scp as being much more secure. Also, code is generally more robust when the server where the data is to end up "pulls" the data it needs, rather than having the source server "push" it to the receiver. But you may not be able to pull this off if you do not control both servers (or cannot gain cooperation from the sysadm) to set things up (firewalling, ssh server, httpd server, ftp server, etc.)

'wget' is another program you could initiate from the receiving server to pull the files.
 
Old 10-06-2006, 07:23 PM   #4
stress_junkie
Senior Member
 
Registered: Dec 2005
Location: Massachusetts, USA
Distribution: Ubuntu 10.04 and CentOS 5.5
Posts: 3,873

Rep: Reputation: 332Reputation: 332Reputation: 332Reputation: 332
Quote:
Originally Posted by haertig
If this is a homework assignment (your description makes it sound like one) you shouldn't be asking for examples here. (1) You don't learn anything, and (2) It's against the rules of the forum.
Now that you mention it I wonder if it is homework. I'm usually pretty suspicious about questions that sound like homework. I hope it isn't. When I read the post it sounded a lot like a project that I had to do in work back in 2003 so I guess that I didn't think about homework because of that.

Last edited by stress_junkie; 10-06-2006 at 07:40 PM.
 
Old 10-09-2006, 05:38 AM   #5
ksakamuri
LQ Newbie
 
Registered: May 2006
Posts: 4

Original Poster
Rep: Reputation: 0
Hi stress_junkie,
First of all thanks for your reply. My question may be sounded like a home work (after you guys response I too realized that ) but truest me its not a home work. If I have time, I might have pulled this out.

Any way coming back to your answer, I am still working on it. and i will post here after i got the final out put. I wish I could use FSTP (ssh) (as you and 'haertig' mentioned) but I don’t have control on the remote server, and we got only 'cd' & 'put' command open on their remote FTP server.

Btw, files are owned by 'weblogic' and ftp has function id. And I am not sure whether I can use .netrc in my case or not.

any way, I would like to thank you again for all your help with detailed script.

Last edited by ksakamuri; 10-09-2006 at 06:00 AM.
 
Old 11-04-2006, 06:23 PM   #6
josenj
Member
 
Registered: Oct 2006
Location: New Jersey (US)
Distribution: Arch Linux
Posts: 58

Rep: Reputation: 15
Check out lftp. You can use it in shell scripting.

Good luck!
 
Old 11-05-2006, 06:52 PM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
I've written Perl subs to send emails & do ftp put. If you're interested, leave a note here.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Which process has ftp on solaris 10? xpucto Solaris / OpenSolaris 3 03-03-2006 04:56 AM
automate ftp (non-GUI) prx Linux - Software 1 04-26-2005 10:14 PM
Kermit Script to Automate FTP SSL/TLS fiddelm3742 Linux - Software 0 05-19-2004 12:53 AM
Automate FTP noodle123 Linux - General 1 10-15-2002 05:50 PM
ftp-download as background process? Steave Programming 3 10-17-2001 05:58 AM


All times are GMT -5. The time now is 06:29 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration