Does anyone know how to create a bash script that would SSH to a list of network devices, via IP addresses, and execute a list of commands?

The way I see this working is as follows:

  • List of IP addresses exist in a text file
  • Commands to be executed exist in a text file
  • Bash script SSH to each IP address and executes commands

NOTE: I have not been able to find a response to this question in the list of other similar questions in this group. I am not a scripter, but would like to be able to be able to create a text file containing the commands that need to be entered on a network device, and then have that file executed on multiple network devices whose IP addresses are located in another text file. It would be great to be able to execute a bash script that allows me to enter a user and pass via SSH, connects to each IP address in the in the referenced text file, executes the command text file, and then echos a success or failure type response in another text file.

It's a bit complicated for me, but I would assume it would be rather straight forward for someone with scripting experience.

I appreciate all of the assistance, and would be willing to pay someone for their time. Just message me.


  • An 'expect' script would probably be a good solution for this, but depending on the type of network devices they are you may be able to get away with just putting your commands in a here doc. – jesse_b Dec 8 '17 at 14:46
  • 1
    pssh (parallel ssh), clusterssh. – Archemar Dec 8 '17 at 14:55
  • 1
    There are a bunch of existing ssh-to-a-bunch-of-remote-systems softwares out there, or see ansible and other such configuration managements. – thrig Dec 8 '17 at 14:55
  • If you're not averse to Python, I've used netmiko very successfully. – Ben Dec 8 '17 at 15:11

Assuming you don't need to enter a password for authentication:

for host in `cat ip-list`
     ssh $host <command-file
  • 1
    you should use cat iplist | while read host; do ssh $host <cmd-file ; done – FaxMax Dec 8 '17 at 15:17
  • 1
    @FaxMax A more verbose script is probably easier for a beginner to understand. And the script is only the basic framework of what needs to be done – PiedPiper Dec 8 '17 at 15:19
  • @FaxMax Why? The only difference I can see that it can handle the case if the concatenated list of the hostnames is longer as the maximal argument list length (which is 512kByte in Linux). I think if the OP would govern so many machines, he wouldn't use such a script for this task. Furthermore, maybe while read host; do ssh "$host" <cmd-file; done < iplist is more optimal, although the difference is negligible. – peterh Dec 8 '17 at 15:33
  • well, as long as everyone knows what the difference between for x in $(cat file) and while read -r x .. < file is if there's whitespace on some lines – ilkkachu Dec 8 '17 at 15:44
  • @ilkkachu The input is supposed to be a list of ip-addresses, There shouldn't be any white-space in there. – PiedPiper Dec 8 '17 at 15:49

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