Walker News

How To Create Auto FTP Script On Linux?

FTP is an insecure file transfer protocol and thus prohibited by most system administrators and IT auditors. The best alternate is using SCP or SFTP to transfer files over SSH protocol.

However, security or vulnerability of FTP is only side info, not the concern of this topic. If you would like to create an auto FTP script that can be scheduled as cronjob to automate file transfer, here you go.

First, there must be a .netrc file in home directory. If this file is not exists, just manually create one:
vi $HOME/.netrc

A sample of .netrc contains 2 sets of auto-FTP information:
machine ftp_ip_or_hostname
login ftp_login_id
password ftp_login_password 
macdef init
prompt 
bi
mput fin*.rpt
quit

machine 192.168.22.45
login walker
password abc123 
macdef init
prompt 
as
mget rpt*.log
quit
 

Where texts in red are mandatory keywords, texts in italic style are user-supplied values for the respective keywords, texts in blue are FTP commands to be executed after login FTP server successfully.
There must be a blank line immediately after the last FTP command (blue texts). Otherwise, you’ll get “Macro definition missing null line terminator.” rejection.

The password is written as plain text in .netrc – one of the obvious reasons you shouldn’t use FTP for file transfer, especially auto-FTP (if can be avoided).

Next, set .netrc permission to 600:
chmod 600 .netrc

Now, the auto-FTP setup is done. Using the .netrc sample (above), when I execute ftp 192.168.22.45 at Linux command prompt, the FTP client will automatically login, set interactive mode to off, switch to ASCII mode, download rpt*.log and then logout!

Custom Search

2016  •  Privacy Policy