Solved

Help with shell script to copy to mulitple servers

Posted on 2015-01-30
4
165 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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

How many times have you wanted to quickly do the same thing to a list but found yourself typing it again and again? I first figured out a small time saver with the up arrow to recall the last command but that can only get you so far if you have a bi…
It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

895 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

13 Experts available now in Live!

Get 1:1 Help Now