Automating FTP
Automating FTP
Automating FTP
AUTOMATIC
DELIVERY
Anna Maria Lopez Lopez
If you find your self executing the same few steps in FTP, youll save
time and effort with a little automation. BY DAVID TANSLEY
64
Using .netrc
Whether you are operating FTP in batch
mode or interactive mode, it is often
better to have a mechanism that lets you
login automatically to a remote site.
Before we start into the details of FTP
automation, I'll begin with a look at
automated login. A special file called
.netrc, which is located in your $HOME
directory, allows you to automate login
in FTP. The file permissions for .netrc
should be set to read/write for the
owner only chmod 600. The file can
contain multiple entries for remote sites.
When a .netrc file is present, if the
machine name given in the FTP com-
W W W. L I N U X - M A G A Z I N E . C O M
password
03 machine uk04lx6003
login
dxtans
password
mas123
SYSADMIN
Automating FTP
Listing 2: ftp1
01 #!/bin/bash
02 # ftp1
03 ftp -i -v <<mayday
04 open uk01lx6001
05 ascii
06 lcd /tmp
07 cd /etc
08 get hosts
09 quit
10 mayday
Getting a File
Files in a List
.netrc Tricks
Listing 4: ftp2
01 #!/bin/bash
02 # ftp2
03 list="hosts hosts.allow hosts.
deny"
05
06
ftp -i -v <<mayday
07
open uk01lx6001
08
ascii
09
lcd /tmp
10
cd /etc
11
get $files
12
quit
13
mayday
18 221 Goodbye.
14 done
W W W. L I N U X - M A G A Z I N E . C O M
do
65
SYSADMIN
Automating FTP
Listing 5: ftp4
01 #!/bin/bash
02 # ftp4
03 netrc_file=$HOME/.netrc
04 if [ -r "$netrc_file" ]
05 then
06
07 else
08
09 fi
Listing 6: ftp5
01 #!/bin/bash
02 # ftp5
15
password=`cat $netrc_file |
awk "NR==$ans"|awk '{print
$6}'`
03 netrc_file=$HOME/.netrc
04 if [ -r "$netrc_file" ]
05 then
06 max_recs=`cat $netrc_file |
awk 'END{print NR}'`
16
else
17
08
19 fi
09
10
13
W W W. L I N U X - M A G A Z I N E . C O M
18
20
read ans
12 then
66
exit 1
#
21 else
22
23 fi
# netrc present
Automating FTP
SYSADMIN
Listing 7: ftp7
01 #!/bin/bash
15
lcd /tmp
02 # ftp7
16
cd /etc
03 log=ftp.log
17
get $files
04 >$log
18
quit
05
19
mayday
06 list="hosts telnet.conf"
20 done
07 host="uk01lx6001"
21
08
09 echo "Script name [ `basename
$0` ]" >>$log
if egrep "202|421|426|450|500|
501|503|550|553|666|777|999" \
then
24
Conclusion
11 do
25
exit 1
12
ftp -i -v
<<mayday
26
else
27
13
open $host
28
14
ascii
29 fi
advertisement
W W W. L I N U X - M A G A Z I N E . C O M
67