1

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.

Thanks,

  • 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 08 '17 at 14:46
  • 1
    pssh (parallel ssh), clusterssh. – Archemar Dec 08 '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 08 '17 at 14:55
  • If you're not averse to Python, I've used netmiko very successfully. – Ben Dec 08 '17 at 15:11

1 Answers1

1

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

#!/bin/bash
for host in `cat ip-list`
do
     ssh $host <command-file
done
PiedPiper
  • 944
  • 1
    you should use cat iplist | while read host; do ssh $host <cmd-file ; done – FaxMax Dec 08 '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 08 '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 08 '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 08 '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 08 '17 at 15:49
  • @PiedPiper, In this case, yes. I know. It's just that you still need to know the difference so as not to trip on it some other time. – ilkkachu Dec 08 '17 at 15:50
  • @ilkkachu read is only useful here if you know the addresses are one per line. cat also works if they are on one line – PiedPiper Dec 08 '17 at 16:00
  • Thanks guys. Yes, I am a beginner with scripting. I have a bunch of network firewalls that I need to update occasionally, and I am tired of logging into each and everyone of them individually just to make simple updates. We do need to user a "username" and "password" to login to these devices. – Jack Nash Dec 10 '17 at 17:02