Script to login to several servers and get files

Hi ppl,
I am looking out for a shell script
a. That would have to login(from a main server) to say 16 servers individually.
b.On each server go to a particular location, check if a particular file is generated on a date(say every sunday it gets generated and i would be interested in the latest one),
c.if the latest file is present, retrieve the file to main server.
d.if the latest file is not present, wait and check later.
e. This has to be repeated until the files from all 16 servers reach the main server.

Exactly the same thing I am looking for.
Not all exactly but login to different server is the 1st part.

I am writing this script. Once the script is created I will share

thansk hiten! how long would that take?

Need gnu date and ssh passwordless set ready.

put the script in cronjob.

LastSun=$(date -d "last sunday" +%Y%m%d)

cd /Main.server/location

while read server
do
  if [[ ! -f file.$LastSun.$server ]] ;
  then 
      scp $server:/remote/location/file.$LastSun file.$LastSun.$server
  fi
done< server.list
1 Like

thanks so much..but could you please elaborate on "Need gnu date and ssh passwordless set ready."

With GNU date, you can easily get the date of last Sunday.

By ssh passwordless set ready, you can execute remote commands on different servers without interrupt.

1 Like

To check for GNU date try the command:

$ date -d 9am

If you get and error about -d being unknown then you don't have gnu date. If it outputs a date string for 9am today your all good.

Check this quick tutorial on setting up and testing passwordless ssh.

1 Like

i am getting this when i had tried the -d option along with date,
date: illegal option -- d
usage: date [-u] mmddHHMM[[cc]yy][.SS]
date [-u] [+format]
date -a [-]sss[.fff]
please help :frowning:

If you have perl and the POSIX module you could do:

LastSun=$(perl -MPOSIX -e '(@date)=localtime;
    my $dow = POSIX::strftime("%u", 0, 0, 0, $date[3] , $date[4], $date[5], -1, -1, -1);
    print POSIX::strftime("%Y%m%d", 0, 0, 0, $date[3]-($dow?$dow:7), date[4], $date[5], -1, -1, -1);')

Some doubt here if LastSun should be 7 days ago or today when current day is a Sunday.