Learn how to run a single command on a number of Linux machines directly

I’m all the time in search of methods to make my work extra environment friendly, so the very last thing I wish to should do is log into a number of Linux machines and run a single command on every. I’d a lot reasonably have the power to run that command on a number of machines directly.
SEE: 40+ open supply and Linux phrases it’s good to know (TechRepublic Premium)
After all, there are apps for that. However since that is Linux, wouldn’t it make extra sense to piece collectively your personal answer? After all, it will. In any case, Linux is about alternative, energy and adaptability.
Say, for instance, you had a number of Ubuntu Servers and also you wished to run sudo apt-get replace and sudo apt-get improve on them abruptly. You don’t should log in to every of these machines to run these instructions, not when there are scripts to deal with that job for you.
I’ve discovered such a script and have modified it in a approach that you should use it to create a number of scripts for particular hosts (corresponding to Ubuntu- or RHEL-based servers). Let’s check out this script and see easy methods to make it work.
What you’ll want
To make use of this script you’ll want a Linux machine to run the command and Linux servers to ship instructions to. Relying on the command you wish to run on the distant servers, you’ll want a consumer with sudo privileges.
That’s it. Let’s make some magic.
Learn how to configure SSH
The very first thing we should do is create an SSH config file that may outline our distant hosts. However there’s a trick to this explicit setup. We’re going to outline hostnames that start with both ubuntu- or rhel-, relying on the working system for every.
On the system you’ll be working the script, create the SSH config file with the command:
nano ~/.ssh/config
Let’s say we’ve the next:
Ubuntu-based techniques on IP addresses 192.168.1.11 and 192.168.1.13 and an RHEL-based machine on IP deal with 192.168.1.14. Our config file may appear to be this:
Host rhel-db1
Hostname 192.168.1.14
Host ubuntu-invoice
Hostname 192.168.1.11
Host ubuntu-portainer
Hostname 192.168.1.13
Save and shut the file.
Learn how to create the script
Now, we’ll create two scripts, one for Ubuntu servers and one for RHEL servers. Create the Ubuntu server script with the command:
nano ~/ubuntu-cmd
In that file, paste the next:
!/bin/bash
# Get the consumer's enter as command
[[ -z $@ ]] && exit || CMD_EXEC="$@"
# Get the hosts from ~/.ssh/config whose names are prefixed by `lan-`
HOSTS=$(grep -Po ‘HostsKubuntu-.*’ “$HOME/.ssh/config”)
# Check climate the enter command makes use of sudo
if [[ $CMD_EXEC =~ ^sudo ]]
then
# Ask for password
learn -sp '[sudo] password for remote_admin: '
password; echo
# Rewrite the command
CMD_EXEC=$(sed "s/^sudo/echo '$password' | sudo -S/"
"$CMD_EXEC")
fi
# loop over the hosts and execute the SSH command, take away `-a` to override the>
whereas IFS= learn -r host
do
echo -e 'n 33[1;33m'"HOST: $host"' 33[0m'
ssh -n "$host" "$CMD_EXEC 2>&1" | tee -a "/tmp/$(basename "$0").$host.>
done <<< "$HOSTS"
Save and close the file. Next, create the RHEL script with the command:
nano ~/rhel-cmd
In that file, paste the following:
#!/bin/bash
# Get the user's input as command
[[ -z $@ ]] && exit || CMD_EXEC="$@"
# Get the hosts from ~/.ssh/config whose names are prefixed by `lan-`
HOSTS=$(grep -Po 'HostsKrhel-.*' "$HOME/.ssh/config")
# Check climate the enter command makes use of sudo
if [[ $CMD_EXEC =~ ^sudo ]]
then
# Ask for password
learn -sp '[sudo] password for remote_admin: ' password; echo
# Rewrite the command
CMD_EXEC=$(sed "s/^sudo/echo '$password' | sudo -S/" <<<
"$CMD_EXEC")
fi
# loop over the hosts and execute the SSH command, take away `-a` to override the>
whereas IFS= learn -r host
do
echo -e 'n 33[1;33m'"HOST: $host"' 33[0m'
ssh -n "$host" "$CMD_EXEC 2>&1" | tee -a "/tmp/$(basename
"$0").$host.>
executed <<< "$HOSTS"
Save and shut the file. Transfer these recordsdata to a listing in your path, corresponding to with the command:
sudo cp ubuntu-cmd rhel-cmd /usr/native/bin
Change the possession of these scripts with:
sudo chown $USER /usr/native/bin/ubuntu-cmd
sudo chown $USER /usr/native/bin/rhel-cmd
We now have our Ubuntu and RHEL hosts outlined in our SSH config file and our scripts which can be globally executable. Let’s first run the Ubuntu script and ship the sudo apt-get replace command to our Ubuntu servers. For that, the command could be:
ubuntu-cmd sudo apt-get replace
The script will examine the SSH config file for any host that begins with ubuntu after which immediate you for each your sudo password and your distant consumer password for every host. It ought to efficiently run the apt-get replace command on any Ubuntu server discovered within the SSH config file.
Subsequent, we’ll run sudo dnf replace on our RHEL hosts with the command:
rhel-cmd sudo dnf replace
The identical factor will occur, solely the dnf replace command will run on our RHEL-based hosts outlined within the SSH config file.
And that’s all there may be to making a script to run a command on a number of hosts outlined in your SSH config file. Use this setup to see what different intelligent issues you are able to do as a result of that’s a part of the ability of Linux.
Subscribe to TechRepublic’s How To Make Tech Work on YouTube for all the newest tech recommendation for enterprise execs from Jack Wallen.