Solved

how to find if exists and file growth using FTP

Posted on 2011-02-18
6
1,568 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 74

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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

744 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now