Results 1 to 2 of 2
hi all,
RHEL 4 64bit.
I need to automate ftp process.I have to ftp backup files from 30 different locations. I need to write a script where I could automate ...
- 06-16-2009 #1Linux Newbie
- Join Date
- Jul 2007
- Posts
- 144
automate FTP from various locations
hi all,
RHEL 4 64bit.
I need to automate ftp process.I have to ftp backup files from 30 different locations. I need to write a script where I could automate the process.All the 30 locations have same username and password to login.
I wrote the following script.Kindly improve/verify it.
#!/bin/bash
USR=usr
PWD=pwd
DATE=$(date)
for i in `cat IP` ; do
#IP is a file containing hostnames and IP.For ex (host1 80.0.0.1)
NAME=$i|awk '{print $1}'
IPADD=$i|awk '{print $2}'
ping -c 1 $IPADD>$NAME.txt
if $NAME.txt|awk '{print $4}' =1 ; then
ftp -n $IPADD<<EOF
user $USR $PWD
bin
ha
mget dump.gz
EOF
else
echo "$NAME is down on $DATE" >$NAME_dwn.txt
done
exit
Its throwing exception at if command saying command not found and then going to the else part straight away.
Kindly guide for the same.
Regards
- 06-16-2009 #2
There are a bunch of problems with the script, not the least of which is the fact that here documents in bash generally do not work for interactive commands like ftp. That's what things like expect are for. I think if you really want to make this work, you'll have to incorporate expect somehow.
The other thing is that you have to change the internal field separator variable in order for the loop to work the way you want it to. Otherwise, the variable i will make the first iteration of the loop "host1", the second iteration of the loop "80.0.0.1", etc. You want one line for each iteration so you have to change the IFS variable to do that. Otherwise, the loop will delimit based on any whitespace.
Third, the output of that ping command will return more than 1 row of text so you can't just print the 4th column of that text and compare it to another value. You'll have to narrow that output down to the row you need. It looks like the 5th row is the one you are after but I will leave that part up to you. Below is a better (but still far from perfect) version of the script. If you run it, you'll get a "too many arguments" error because of the problem I just described.
Code:#!/bin/bash #set -x IFS=$'\n' USR=usr PWD=pwd DATE=$(date) for i in `cat IP` ; do #IP is a file containing hostnames and IP.For ex (host1 80.0.0.1) NAME=`echo $i | awk '{print $1}'` IPADD=`echo $i | awk '{print $2}'` ping -c 1 $IPADD > $NAME.txt if [ `awk '{print $4}' $NAME.txt` -eq "1" ]; then ftp -n $IPADD << EOF user $USR $PWD bin ha mget dump.gz EOF else echo "$NAME is down on $DATE" >$NAME_dwn.txt fi done


Reply With Quote