3

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 .

Manish
  • 85

1 Answers1

10

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 02 '16 at 02:53
  • @Manish Added password support using "sshpass" program. – DepressedDaniel Dec 02 '16 at 03:02