1

I want to create a script to replace one file in listed CentOS systems under /usr/bin.

Eg: I need to Copy & Replace one file from a central location to multiple systems, (using root login).

path : /storage1/data/client /usr/bin/

Can anyone help with this?

My list sample,

10.20.1.2
10.20.1.3
10.20.1.4
2

How about using scp in a for loop?

for server in user1@server1 user2@server2 user3@server3
do
    scp /storage1/data/client "$server":/usr/bin/
done

If the servers are already in an array, you would use:

#!/bin/bash
for server in "${servers[@]}"
do
    scp /storage1/data/client "$server":/usr/bin/
done

If in a file (single server per line), you would write:

while IFS= read -r server
do
    scp /storage1/data/client "$server":/usr/bin/
done < serverfile

Of course you can manually add the root user if that is user you want to connect with (this also strips leading and trailing whitespace), like so:

while read server
do
    scp /storage1/data/client root@"$server":/usr/bin/
done < serverfile

If the number of servers is too large you could do the copying concurrently, like so:

for server in user1@server1 user2@server2 user3@server3
do
    scp /storage1/data/client "$server":/usr/bin/ &
done
wait
| improve this answer | |
  • I've more than 100 systems, how Can I use a list in this. – BloodHunter Jun 7 '19 at 5:40
  • @BloodHunter: What is a "list"? Do you mean an array, a file, something else? – user000001 Jun 7 '19 at 5:41
  • a file with hostnames or ip's – BloodHunter Jun 7 '19 at 5:48
  • @BloodHunter: See edit then on how to read the file line by line, if you can't make it work edit the question to add a sample of the file to show the precise format. – user000001 Jun 7 '19 at 5:51
  • @BloodHunter: The fourth script should work with your file, and connect to all servers as root (assuming root login over ssh is enabled - that's not always the case). – user000001 Jun 7 '19 at 5:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.