© Stanislav Rishnyak, 123RF.com
Managing computers with Rex
King of Computers
Rex gives administrators the ability to manage all the machines on a network from a central location. The only precondition is that all of the computers must be reachable via SSH, and the admin computer also needs to have a Perl environment installed. On your own computer, you can then define a task that selected computers, or all of them, will run.
In this way, you can retroactively install and delete programs, handle maintenance, change the configuration of all your workstations uniformly, distribute documents, or simply query health state information for your systems.
With a correspondingly formulated task, you can simply issue, for example, the rex apache2 command to install apache2 on all your web servers and kit them out with the appropriate configuration files at the same time.
By the way, if you ask the programmer, Rex [1] should actually be called (R)?ex. But, because this is difficult to read, write, and pronounce, I will simply use Rex in this article.
Freedom of Choice
Rex can manage computers running the Linux distributions CentOS 5 and 6, Debian 5 and 6, Fedora, Gentoo, Mageia, openSUSE, RHEL 5 and 6, Scientific Linux, and Ubuntu 10.04, 11.04, and 12.04. Additionally, it can handle Solaris (SunOS) and BSD systems. Although Rex does not actively block other Linux distributions, you should keep your fingers crossed when you activate a task.
Rex itself is only needed on the administrator's own machine, which even can be a Windows or Mac OS X machine; the only important thing is that you have a Perl environment in place. To install Rex, first pick up the git versioning tool (using the package manager on Linux) and add the make tool. Then, access GitHub and create the Makefile you will need for the installation:
git clone https://github.com/krimdomu/Rex.git cd Rex perl Makefile.PL
Very likely at this point, you will see a few missing Perl modules scroll across the screen – on Linux, you can typically install them with the package manager. Then, you can launch into the actual installation with the commands:
make sudo make install
Typing rex -version should now output a version number. If not, something has gone wrong with the installation, probably because some required Perl packages are not in place.
For all supported Linux distributions, the developers also provide repositories with prebuilt Rex packages. How you bind the repositories depends on your distribution. If you are unlucky, this approach will involve more typing than installing directly from the sources as presented here. On the upside, Rex is kept up to date automatically – well, more or less. When this issue went to press, the contents of the repositories lagged behind the Rex version from GitHub. Anyone who would like to use the repositories can find the installation instructions online [2].
As soon as Rex is ready for use, you should briefly check that all the machines on the network are reachable via SSH. If you can log on to any computer with the same user credentials, this will make writing the tasks much easier.
Mileage
For a first, short test, Rex can determine how long a computer on the network has been running. For example, to check the uptime for two computers named marvin and zaphod, you would enter:
rex -e "say run 'uptime'" -H "marvin zaphod"-u hans -p 123456
The -e parameter is followed by the command to be run in quotes. run 'uptime' tells rex to start the uptime program on the remote computer. The say to the left of this ensures that the uptime output is sent to the administrator's terminal. The -H parameter lists all of the affected computers in quotes. Instead of using the hostnames, you can use the IP addresses as shown in Figure 1. The username required to login to both computers follows -u and the password -p.
Figure 1: On the first computer, the login fails, which leads to the cryptic error message at the end of the results, among other things. However, the second computer reveals its uptime.
To avoid excessive typing in the case of more complex tasks, you can also place all of the necessary information in a text file, known as the Rexfile. To query the uptime, you would fill the file with the content from Listing 1.
Listing 1
A Rexfile with a Task
01 user "Hans";
02 password "123456";
03
04 desc "Uptime estimate";
05 task "uptime", sub {
06 say run 'uptime';
07 };
At the start of the listing, after user and password, you can see the username and password in quotes; the administrator uses these credentials to log on to all the machines via SSH. The task definition follows, and what the task does is shown in the short human-readable description in quotes that follows desc. The task line assigns a unique name to the task – in Listing 1, it is simply named uptime.
What Rex needs to do when the task is called is stated between the curly brackets. In Listing 1, run starts the uptime program on the external computers and sends the output to the administrator's terminal (say). The end of each line or instruction is marked by a semicolon.
You can run the defined task with the following command:
rex -H "marvin zaphod" -f uptime.rex uptime
The -H option is again followed by the computers on which the uptime task is to be run. rex expects the task definition in a file called uptime.rex. If you rename uptime.rex to Rexfile, you can save the bother of specifying the -f parameter.
Without the -H parameter, rex would run the action locally, that is, on the administrator's machine. This means you could also misuse Rex for automating local tasks, such as building a program or compiling documentation. All parameters for rex must always be entered before the task name (in the example above, before uptime). Otherwise, rex ignores them.
Security-aware administrators will be up in arms when they look at Listing 1. The Rexfile really does contain the username and the password in plain text. If you leave them out, you need to pass them into rex again with the parameters -u and -p. The version of rex that was available when this issue went to press didn't always reliably interpret these two parameters. As another alternative, you can also use SSH keys to authenticate. The following lines handle this:
user "hans"; private_key "/path/to/private.key"; public_key "/path/to/public.key";
You need these lines at the start of the Rexfile with the appropriate paths. If the keys are in the default directories (below $HOME/.ssh), it is sufficient to specify the user.
Groups
If you have to manage many computers, you can group them in the Rexfile:
group "intranet" => "marvin", "zaphod";
This line must occur before the first task. This example creates a new group called intranet, which contains two computers: marvin and zaphod. If the hostnames contain a sequential number, such as client01 through client05, you can use a shorter notation:
group "intranet" => "client[01..05]";
When you call rex, you just need to specify the group:
rex -G "intranet" uptime
for all the computers to run the uptime action. You could also leave this detail out if you restrict the uptime task to the intranet group in the Rexfile:
task "uptime", group => "intranet", sub { ...The reduced-to-the-max command line is now: rex uptime. Multiple groups are listed in square brackets:
task "uptime", group => ["intranet", "accounting"], sub { ...Instead of listing groups, you can do:
task "uptime", "marvin", "zaphod", sub { ...to simply list the affected computers.
Buy this article as PDF
(incl. VAT)
Buy ADMIN Magazine
Subscribe to our ADMIN Newsletters
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Most Popular
Focus On Self-Hosting
• Running Your Own Matrix Server with Synapse
• Reliable Network Storage with TrueNAS Community Edition
• Self-Hosted File Syncing with Seafile
• Wiring the Modern Stack with Node-RED
• Self-Hosted Collaboration with Forgejo
• Self-Hosted PaaS with Coolify
• Build and Host Docker Images
• Self-Hosted Pritunl VPN Server with MFA
Support Our Work
ADMIN content is made possible with support from readers like you. Please consider contributing when you've found an article to be beneficial.
