Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Help with shell script to copy to mulitple servers

Posted on 2015-01-30
4
Medium Priority
?
185 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 20

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 20

Accepted Solution

by:
simon3270 earned 2000 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

Does Your Cloud Backup Use Blockchain Technology?

Blockchain technology has already revolutionized finance thanks to Bitcoin. Now it's disrupting other areas, including the realm of data protection. Learn how blockchain is now being used to authenticate backup files and keep them safe from hackers.

Question has a verified solution.

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

Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
Fine Tune your automatic Updates for Ubuntu / Debian
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.:
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Suggested Courses

670 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