I was asked in an interview how to run commands on hundreds of servers using just a plain Bash script. Initially, I thought that meant I had to install Ansible on each node, but that seemed impractical. I suggested using the SSH-keygen utility for authentication. Was that on the right track?
5 Answers
SSH-keygen generates authentication keys for SSH, but it's just one part of the overall picture. You'd need to think about how you'd actually distribute those keys and connect to your servers. You might want to consider using a for loop to send commands to each server after setting up the keys. Think of it like this: if your servers are all set up with SSH keys, you can easily run a command on each using a simple loop.
Remember, while a simple for loop works, using tools like "pdsh" or "parallel-ssh" can speed things up if you're dealing with a large number of servers. But it sounds like the prompt was specifically about sticking to Bash only! Keep experimenting with your ideas!
I've heard of those tools! They're quite efficient for mass command execution.
Honestly, saying SSH-keygen in an interview might raise some eyebrows. It could signal a lack of understanding about the broader topic of server management and remote execution. You'd want to explain how you'd get access first, how you're using those keys, and what comes after the initial connection.
Would they really think less of someone for mentioning keygen, though?
In essence, you need both ssh access and a method to manage your tasks. Sticking a script on the remote machine and calling it via SSH is another approach. For example, save your commands in a script, and then execute it like this:
```
for host in "${hostlist[@]}"; do ssh "user@$host" "/path/to/script.sh"; done
```
Make sure to handle output logging if necessary!
That's a neat idea! Logging can save you a lot of headaches.
If your keys are set up and you don't need to execute everything at once, a for loop is your best bet. Something like:
```
for node in {1..500}; do ssh user@node$node "uptime"; done
```
This will let you run the command in sequence. If you want to parallelize the execution, just add an ampersand at the end of the command.
True! Just keep in mind that running too many processes simultaneously can overwhelm the servers.

What about this approach?
```
ssh-keygen -f masterkey
for i in "${hostlist[@]}"; do ssh-copy-id "user@$i"; done
```
Isn't that enough to get started?