Solved

how to find if exists and file growth using FTP

Posted on 2011-02-18
6
1,581 Views
Last Modified: 2012-05-11
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.
0
Comment
Question by:vkchaitu82
  • 3
  • 2
6 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 34930341
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.
0
 

Author Comment

by:vkchaitu82
ID: 34930353
Both are Linux servers and have Korn shell
0
 
LVL 19

Expert Comment

by:simon3270
ID: 34930355
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

0
ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 

Author Comment

by:vkchaitu82
ID: 34935172
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..
0
 
LVL 19

Accepted Solution

by:
simon3270 earned 500 total points
ID: 34937886
What was the error?

I am using one Ubuntu machine to talk to another, and "ls -tr" on its won seems to work fine (produces a list of the files, in date order).  I think the problem you would have is that "ls -tr $REMFILE" doesn't work (it tries to write the list of all files in the S2 directory to a file called $REMFILE on S1).

One option would be to change ftpgetdir to do "ls -tr" instead of "dir $REMFILE", then to search the output for the desired file.  You then get the name for that file using the "REMNAME=$(awk '{print $NF}' /tmp/dirf.$$)" command, and use *that* filename on the "get" line in getftpfile().
0
 

Author Closing Comment

by:vkchaitu82
ID: 34980955
This is perfect and works..Thanks for your time...
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The purpose of this article is to demonstrate how we can use conditional statements using Python.
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question