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 |
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 The abcftp.sh file should contain this: Code:
#! /bin/bash Code:
chmod u+x abcftp.sh Code:
machine remote.computer 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 Code:
/home/abcftp/abcftp.sh 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 Code:
man 5 crontab Code:
* 20 * 1-5 /home/abcftp/abcftp.sh > /home/abcftp/abcftp.log 2>&1 Let us know if you succeeded or if you need help. |
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. |
Quote:
|
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. |
Check out lftp. You can use it in shell scripting.
Good luck! |
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. |