Link to home
Start Free TrialLog in
Avatar of sigma19
sigma19Flag for United States of America

asked on

how to find if exists and file growth using FTP

Hi experts,

I have server 1 (S1), server 2 (S2) (Data resides here)

I should connect from S1 using shell script to S2 using FTP.The follwing conditions should be checked after the connect was made.

1) Check if the required files exists (example abc.txt).If not write a log to S1 that they do not exist.
2) If file exists then check the file size for every 2 mins  to see if its growing.And if its not growing then move file from S2 to S1 and write the log in S1 and exit.

I cant create scripts or any logs in S2.
Avatar of kaufmed
kaufmed
Flag of United States of America image

You didn't give any information as to your environment. What kind of systems are you running on/against? Perhaps python would be a good, simple-to-implement solution for what you seek to do.
Avatar of sigma19

ASKER

Both are Linux servers and have Korn shell
From S1, set the host (S2) you are connecting to, the user and password on S2, and the file name and directory (use "." for the directory if it is in the S2 user's home directory).

Then run the script on S1.  It first checks that the file exists on S2 and exits if it doesn't.  If it does, it monitors the file size very 120 seconds - if the size (in bytes) doesn't change, it retrieves the file and exits.
REMFILE=remote.file
REMDIR=/var/log/adm
REMUSER=u1
REMPASS=secret
REMHOST=S2

getftpdir() {
ftp -n $REMHOST 2>/dev/null <<EOF
user $REMUSER $REMPASS
cd $REMDIR
dir $REMFILE
quit
EOF
}

getftpfile() {
ftp -n $REMHOST 2>/dev/null <<EOF
user $REMUSER $REMPASS
cd $REMDIR
get $REMFILE
quit
EOF
}

# Get first entry to check existence, and initial size
getftpdir > /tmp/dirf.$$

if grep -i "no such file" /tmp/dirf.$$ >/dev/null 2>&1
then
  echo ERROR: File $REMDIR/$REMFILE does not exist on host $REMHOSTA for user $REMUSER
  rm /tmp/dirf.$$
  exit 1
fi

REMSIZE=$(awk '{print $5}' /tmp/dirf.$$)
REMNAME=$(awk '{print $NF}' /tmp/dirf.$$)

if [ "$REMNAME" != "$REMFILE" ]
then
  echo ERROR: File name $REMNAME does not match expected $REMFILE
  rm /tmp/dirf.$$
  exit 1
fi

while true
do
  sleep 120
  getftpdir > /tmp/dirf.$$
  if grep -i "no such file" /tmp/dirf.$$
  then
    echo ERROR: File $REMDIR/$REMFILE on host $REMHOSTA has been deleted while processing 
    rm /tmp/dirf.$$
    exit 1
  fi
  NEWSIZE=$(awk '{print $5}' /tmp/dirf.$$)
  if [ $NEWSIZE = $REMSIZE ]
  then
    # file has not grown in 120 seconds - retrieve it
    break
  fi
  # File has changed size - remember the new size
  REMSIZE=$NEWSIZE
done

getftpfile

rm /tmp/dirf.$$

exit 0

Open in new window

Avatar of sigma19

ASKER

Hi simon3270,

Thanks for the solution. One more question...Is there a way to find get the First file that came in the directory  if there are multiple files present .

Example S2
filename      Creation date
file1.123      1/1/2011
file1.456      1/2/2011

I need the first file as it came first

I tried to use ls -tr in the FTP and taking the output and doing head -1 but its giving error..
ASKER CERTIFIED SOLUTION
Avatar of simon3270
simon3270
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sigma19

ASKER

This is perfect and works..Thanks for your time...