I have a file (lets say "thisfile.sh" for example) I would like to copy to multiple servers (of which some may require password authentication) at once upon launching my bash file, presumedly with SCP. All the server addresses are written one line at a time in a text document, lets say the file's called "ServerList", for example:


How can I achieve this?

Edit: I thought about this a little more, and I'd still like to get this done with bash as a starter, I'll use an alternative a little later. How'd I suppose I'd get it done is by using the SCP copy command, and a variable in place of my destination, a little something like this scp myfile $Server:~/myfile. This way, I'm pretty sure I will need to use a Loop of some sort, so it would go through all the lines as I copy it.

  • Do you want to write the passwords manually? Is the destiny directory always the same? – Hewbot Aug 6 '15 at 15:35
  • For now as I'm only using what I'm programming now as a base, I'd be alright with it pausing to prompt for the password once it encounters a password-protected server. – user3125996 Aug 6 '15 at 16:00

If you want to stay in bash-only domain:

while read server
    scp thisfile.sh $server:/some/location
    ssh $server /some/location/thisfile.sh
done < servers.txt

This will prompt execute one work at a time, and ask you for authentication when needed

  • I just deleted my answer since yours is the same but more complete ;) – Hewbot Aug 6 '15 at 16:18

There are a few ways that you could accomplish this. You can use something like ansible or fabric to write a script that will connect to all of your computers and copy the file to them and run the script. or you can write a bash/shell script to copy the files up there and then run the script.

Fabric and Ansible have a slightly higher learning curve but can be more useful and easily add more computer or change the script.

The bash or shell scrip will be easier to write but a bit more difficult to adapt if you need it in the future.

https://www.digitalocean.com/community/tutorials/how-to-use-fabric-to-automate-administration-tasks-and-deployments -- Fabric

http://docs.ansible.com/ansible/intro_getting_started.html -- Ansible

Here the guides for ansible and fabric


While Felipe has the best answer for a bash only solution, I think that Ansible is far superior for this task.

You simply need to put all of your servers into an inventory file like this:

firstserver.com     ansible_ssh_user=firstuser
secondserver.com    ansible_ssh_user=seconduser
thirdserver.com     ansible_ssh_user=thirduser

And a playbook that looks like this:

--- #copyfiles.yml
hosts: all
- name: copy file to servers
    src: /path/to/local/file
    dest: /path/to/remote/file

And then run:

ansible-playbook -i /path/to/inventory copyfiles.yml --ask-pass

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.