Solved

Help with shell script to copy to mulitple servers

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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Unix process listing into CSV format 3 87
AWK: Pytthagoras bp script Part deux 22 50
postfix start error! 9 86
bash: ./Facebooker.pl :Permission Denied 9 58
Little introduction about CP: CP is a command on linux that use to copy files and folder from one location to another location. Example usage of CP as follow: cp /myfoder /pathto/destination/folder/ cp abc.tar.gz /pathto/destination/folder/ab…
Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

710 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