1

This question already has an answer here:

I have file called server.txt

Suppose it has below servers , there could be more servers server1 server2 server3 server4

how can I copy file (file.txt on all servers using scp command) at /tmp/ location .

marked as duplicate by Gilles 'SO- stop being evil' shell Dec 2 '16 at 21:46

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

6

Assuming bash:

for server in $(cat server.txt)
do
  scp file.txt "$server":/tmp/
done

Parallel mode (don't use this if server.txt is massive as you can run out of bandwidth, stall the connections, and then have a hard time figuring out what succeeded and what failed):

for server in $(cat server.txt)
do
  scp file.txt "$server":/tmp/ &
done
wait

As requested, now with password support:

while read SERVER PASSWORD
do
  sshpass -p "$PASSWORD" scp file.txt "$SERVER":/tmp/
done <./server.txt

Put, in server.txt, the first server's hostname, space, the first server's password on the first line and so on.

  • Thanks Daniel it worked fine , But Everytime I need to Provide a password to connect to server1 , server2 , server3 ,etc . is there a way i can store a password in script ? so that i don't need to provide password – Manish Dec 2 '16 at 2:53
  • @Manish Added password support using "sshpass" program. – DepressedDaniel Dec 2 '16 at 3:02

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