Need help with script to copy code to multiple servers


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help with script to copy code to multiple servers
# 1  
Old 01-27-2015
Need help with script to copy code to multiple servers

Hi,

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 ...


Code:
#
# 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 file looks

Code:
server1;/usr/copycodehere/code1~/usr/copycodehere/code2
server2;/usr/copycodehere/code1~/usr/copycodehere/code2
server3;/usr/copycodehere/code1~/usr/copycodehere/node2


Last edited by Don Cragun; 01-27-2015 at 07:19 PM.. Reason: Add CODE tags.
# 2  
Old 01-27-2015
Code:
(
IFS="$IFS;~"
while read s a b
do
  (
    scp -r a s:b
    echo scp exit code: $?
   ) 2>&1 | sed "s|^|$a=>$s:$b |" &
done <serverlist ) 2>&1 | tee logfile

Add your separators to $IFS for read, read the file line by line into three variables, copy in the background, and use the stdout/stderr of all the bg copies as both a log and a test of their exiting, prefix every log line by the serverlist line info.

Some ssh installs I have known barf when you spawn too many ssh operations too often or at once. You can search the site for 'bctl' to see how to control the parallelism, and add sleep if necessary to keep the peace.

Last edited by DGPickett; 01-28-2015 at 09:07 AM..
# 3  
Old 01-27-2015
Please use code tags as required by forum rules!

Indenting sometimes helps in reading / understanding pieces of code.
See some comments on your snippet:
Code:
case "$CHOICE" in
        y|yes|Yes) while read line
                        do serverName=echo $line | awk -F':' '{print $1}'       # there's not a single ':' in $line; do you mean ';'?
                           distLocations = echo $line | awk -F':' '{print $2}'  # same
                           done                                                 # no redirection: reads from stdin

                for distLocations in ${distLocations//:/ } ;                    # may work, but do you really want use the identical variable name?
                                                                                # and, sure you want to use the colon? 
                                                                                # I see tildes separating the locations in your file
                        do scp -rp $dir $serverName:$distLocations
                done < $HOSTS                                                   # this redirection does not do anything as there's no ready
                ;;
        *) echo "wrong entry"
                ;;
esac

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script to ping multiple servers

Hi I did the following script to ping multiple servers, but I keep on receiveing duplicate emails for one server that is down: #!/bin/bash date cat /var/tmp/servers.list | while read output do ping -c 1 "$output" > /dev/null if ; then echo "node $output is up" else ... (10 Replies)
Discussion started by: fretagi
10 Replies

2. Shell Programming and Scripting

Run a script on multiple servers

I need to run a script on a bunch of remote servers. how can this be done without ssh into each individual server and run it its under /sbin/script.sh on each server (1 Reply)
Discussion started by: tdubb123
1 Replies

3. Shell Programming and Scripting

Script to overwrite & before that keep copy a file on many servers

I have ssh password less auth enable & script does the job well as well #/bin/bash for i in `cat ip` do scp /etc/resolv.conf root@$ip done But I need to take backup of the file i will overwrite .. is there any simple way ? Kindly respond (5 Replies)
Discussion started by: heman96
5 Replies

4. UNIX for Dummies Questions & Answers

Running the same remote script on multiple servers

Experts, Im trying to remote into a server, run a script that resides on that server and capture the information displayed & store in a local file. I struggled with this yesterday & finally that script is working now. Now, here is a scope creep and the script that I wrote for 1 remote... (2 Replies)
Discussion started by: OMLEELA
2 Replies

5. Shell Programming and Scripting

shell script to take input from a text file and perform check on each servers and copy files

HI all, I want to script where all the server names will be in a text file like server1 server2 server3 . and the script should take servernames from a text file and perform copy of files if the files are not present on those servers.after which it should take next servername till the end of... (0 Replies)
Discussion started by: joseph.dmello
0 Replies

6. Shell Programming and Scripting

Need a script to run on multiple mail servers..

Hello, I am a Unix newbie and I need a script in which I can run a command on multiple servers at work. The command is to start a storage process and I am sick of doing it manually on all servers.. Here's the command: /opt/bss/bin/snmptable -CB -v2c -c P67LzuBm hostname hrStorageTable... (4 Replies)
Discussion started by: kinyyy
4 Replies

7. Shell Programming and Scripting

login into multiple servers through script is having some problem

Hi Everybody, I am bit new to shell scripting. I need some help in my script. I have to login into 15 servers and check some logs daily. For that I've written one shell script, somewhere it is having some problems. After log into the first server, the script is not going with the next steps.... (6 Replies)
Discussion started by: raghu.iv85
6 Replies

8. Shell Programming and Scripting

Script ftp multiple servers

Hi guys , i have 1 problem and no find what is the problem...:confused:, and .netrc is configured and correct permissions... REMOTE="/home/user" LISTADO=`cat /root/home/user/LISTADO.txt` MACHINE=$(echo $i|awk 'FS="|" {print $1}') for i in $LISTADO do ftp $MACHINE <<TER passive prompt... (2 Replies)
Discussion started by: Esquizo000
2 Replies

9. Shell Programming and Scripting

Script to SCP a file to multiple servers

Hi All, I am a total noob to the Unix world, and i hope to learn a lot from this wonderful community. Here's my first post and question , i am trying to SCP a file to multiple servers (multiple destinations) through this little script : #!/bin/ksh # copy files # File to be copied... (7 Replies)
Discussion started by: rdlover
7 Replies

10. Shell Programming and Scripting

login into multiple servers thru script...

I need to login into multiple servers thru a script run couple commands and run find command as root. I only have ssh access to the servers as a user than I can "su" to root. If you have a similar script please post it. Also if you can suggest commands that I should consider please let me know. ... (1 Reply)
Discussion started by: avcert1998
1 Replies
Login or Register to Ask a Question