LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to automate an FTP process? (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-automate-an-ftp-process-490022/)

ksakamuri 10-06-2006 10:30 AM

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

stress_junkie 10-06-2006 05:25 PM

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.

haertig 10-06-2006 05:42 PM

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.

stress_junkie 10-06-2006 06:23 PM

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

ksakamuri 10-09-2006 04:38 AM

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.

josenj 11-04-2006 05:23 PM

Check out lftp. You can use it in shell scripting.

Good luck!

chrism01 11-05-2006 05:52 PM

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


All times are GMT -5. The time now is 11:27 PM.