Solved

Help with shell script to copy to mulitple servers

Posted on 2015-01-30
4
168 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
ID: 40580119
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
ID: 40580531
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
ID: 40580661
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
ID: 40587334
Hi Simon sorry for the delay. Your suggestions did help a lot. Thanks for your help the script is working.
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Suggested Solutions

Over the last ten+ years I have seen Linux configuration tools come and go. In the early days there was the tried-and-true, all-powerful linuxconf that many thought would remain the one and only Linux configuration tool until the end of times. Well,…
Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
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.

772 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