Solved

Help with shell script to copy to mulitple servers

Posted on 2015-01-30
4
161 Views
Last Modified: 2015-02-03
I am new to scripting and i am trying to use below script to copy code to multiple servers and multiple locations on each server. the script is not working or doesnt give any error. Any help is appreciated. basically i want a script to get the code from a location (dir below) and read the serverlist file (it has multiple servers and multiple locations on each server ) and copy code to all those servers and locations mentioned on serverlist file. Please help ...

#
# Edit desst variable with directory path where you want file to be copied.
#
HOSTS=/opt/serverlist
dir=/usr/newcode/
echo "Are you sure you want to copy $dir to the  standard list of servers"
echo "This may take a while!!"
echo -n "Enter 'y' or 'n':"
read CHOICE
case "$CHOICE" in
        y|yes|Yes) while read line
do
         serverName=echo $line | awk -F':' '{print $1}'
         distLocations = echo $line | awk -F':' '{print $2}'
done
for distLocations in ${distLocations//:/ } ;
do
           scp -rp $dir $serverName:$distLocations
done < $HOSTS
  ;;
        *) echo "wrong entry"
            ;;
esac


here is how my serverlist looks like
server1;/usr/copycodehere/code1~/usr/copycodehere/code2
server2;/usr/copycodehere/code1~/usr/copycodehere/code2
server3;/usr/copycodehere/code1~/usr/copycodehere/node2
0
Comment
Question by:Ron0612
  • 2
  • 2
4 Comments
 
LVL 19

Expert Comment

by:simon3270
Comment Utility
Yoou had your first "done" too early, and I've added a "< /dev/null" to make sure that scp doesn't read stdin, so swallow extra HOSTS entries.
HOSTS=/opt/serverlist
dir=/usr/newcode/
echo "Are you sure you want to copy $dir to the  standard list of servers"
echo "This may take a while!!"
echo -n "Enter 'y' or 'n':"
read CHOICE
case "$CHOICE" in
        y|yes|Yes) while read line
                     do
                          serverName=echo $line | awk -F':' '{print $1}'
                          distLocations = echo $line | awk -F':' '{print $2}'
                          for distLocations in ${distLocations//:/ } ;
                          do
                              scp -rp $dir $serverName:$distLocations < /dev/null
                          done
                     done < $HOSTS
  ;;
       n|no|No) echo "OK, I won't do it"
            ;;
      *) echo "wrong entry"
            ;;
esac

Open in new window

0
 

Author Comment

by:Ron0612
Comment Utility
Hi Simon,

I tried with what you suggested, i get below error. does it mean it is reading serverlist file properly ?


server1;/usr/copycodehere/code1~/usr/copycodehere/code2: No such file or directory
distLocations: command not found
0
 
LVL 19

Accepted Solution

by:
simon3270 earned 500 total points
Comment Utility
You've got a ";" after the server name - should be a ":".

I'm afraid I just sorted out the looping problems and didn't look closely at the rest of the code.  The lines that set serverName and distLocations need $(..) round the "echo | awk" statements, so that the echo and awk commands are run, and the output from those commands is assigned to the variables.  Also you must not have any spaces round the "=" sign:

    serverName=$(echo $line | awk -F':' '{print $1}')
    distLocations=$(echo $line | awk -F':' '{print $2}')

Then in the next "for" statement, I think you want to break up the list on hyphens, not colons, you cannot use the same name for the for loop control variable as the name with the list of locations, and you don't need the ";" at the end of the line, so:

    for distLocation in ${distLocations//-/ }

Then the scp line becomes:

    scp -rp $dir $serverName:$distLocation < /dev/null

So, the entire script becomes:
HOSTS=/opt/serverlist
dir=/usr/newcode/
echo "Are you sure you want to copy $dir to the  standard list of servers"
echo "This may take a while!!"
echo -n "Enter 'y' or 'n':"
read CHOICE
case "$CHOICE" in
        y|yes|Yes) while read line
                     do
                          serverName=$(echo $line | awk -F':' '{print $1}')
                          distLocations=$(echo $line | awk -F':' '{print $2}')
                          for distLocation in ${distLocations//-/ }
                          do
                              scp -rp $dir $serverName:$distLocation < /dev/null
                          done
                     done < $HOSTS
  ;;
       n|no|No) echo "OK, I won't do it"
            ;;
      *) echo "wrong entry"
            ;;
esac

Open in new window

0
 

Author Comment

by:Ron0612
Comment Utility
Hi Simon sorry for the delay. Your suggestions did help a lot. Thanks for your help the script is working.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Linux users are sometimes dumbfounded by the severe lack of documentation on a topic. Sometimes, the documentation is copious, but other times, you end up with some obscure "it varies depending on your distribution" over and over when searching for …
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…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

762 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

8 Experts available now in Live!

Get 1:1 Help Now