Inquiring minds

Shell interface to the host database: The BSD capability database package defines a file format and provides a library interface and a command to generate a .db file from the text representation, but a shell command to select and display records from a database does not exist. To fill this gap, we have written a new command, hostcap, to access the host database from a shell or from within a scripting language such as Perl.

General purpose database interface: Despite its name, hostcap is not specific to the host database. Neither the capability names nor anything else specific to our particular use of the capability database is hardwired into the program; the list of capabilities as well as their textual descriptions (in different languages) are read from special ``meta-records'' that are present in the host database.

Using hostcap: If hostcap is called with one ore more hostnames, it just lists all the capabilities of the hosts in a readable form, one capability per output line:

   % hostcap ruin
   Name:                         ruin
   Nicknames:                    www-rn
   Comment:                      Server and gateway for RN
   Hardware Architecture:        sparc
   Operating System:             solaris2.5
   Model:                        SPARCstation 20
   Location:                     MZH 5180
   Interfaces:                   +ruin, +ruin-sn
   Display Pixel Depth (bits):   8
   Memory Size (MB):             96
   Processor Speed (MHz):        60
   Serial Number:                445H1742
   Number of CPUs                2
   Normal users can login:       1
   Trusted Host:                 1
   Central Maintenance:          1
   Organization:                 +rn
   %

Note that the name, nicknames, and comment, which are taken from each record's name field, are treated as if they were ordinary capabilities. Boolean capabilities, if present in a record, are listed with a value of 1 and otherwise omitted (or shown with a value of 0, depending on a flag that can be set for each boolean capability in the meta-records).

Using hostcap in scripts: In addition to the verbose output format intended for the human reader, hostcap provides a second output format more suitable for use from within shell scripts or Perl scripts. In this format, all the capabilities (just the values) are printed in a single line separated by colons, one line for each host. Empty fields are printed for (non-boolean) capabilities that are not present in a record, so that the number of fields is always the same in each output line. This format can be post-processed easily using the capabilities of Perl or sed and awk:

   % hostcap -s refrain regen
   refrain:::mips:irix5.3:Indy:MZH 5180:134.102.218.9::8:1280x1024:32:132:::net:1:1:1:+rn::
   regen:::sparc:solaris2.4:SPARCstation 20:MZH 5220:134.102.200.88::8:1:64:50::2::1:1:1:+rn::
   %

Selecting capabilities: To select individual fields to be printed for each record, hostcap can be passed capability names as arguments, each with a trailing colon. This works for both output formats:

   % hostcap name: memory: ruin regen
   Name:                         ruin
   Memory Size (MB):             96

   Name:                         regen
   Memory Size (MB):             64

   % hostcap -s name: arch: os: ruin refrain
   ruin:sparc:solaris2.5
   refrain:indy:irix5.3
   %

Selecting records: In addition to the arguments that select capabilities, hostcap can be passed arguments that select records (hosts) to be displayed. These can be plain hostnames or relational expressions that describe a subset of the hosts in the database. Relational expressions are typically used to find the answers to questions such as

Which hosts of type SPARC belong to the Network Research Group?
On which PowerPCs running Solaris 2.5 is login restricted?
Which hosts located on the 5th floor in the MZH building have more than 32 MBytes memory?

Here are the corresponding calls to hostcap:

   hostcap arch==sparc org==+rn

   hostcap arch==powerpc os==solaris2.5 login==0

   hostcap "room=~MZH 5" "memory>32"

(Relational expressions are explained in detail in the manual page for hostcap.) Arguments that select capabilities can be mixed freely with arguments that select records:

   hostcap arch!=powerpc org==+zentral name: memory:

Since hostcap is frequently used to just list the names of the hosts that satisfy a certain condition, an additional command hostnames is offered as a shorthand for ``hostcap -s name:'':

   % hostnames arch==sparc os=~solaris2 org==+rn
   ruin
   regen
   refrain
   %

Using hostcap in distfiles: We have added a simple extension to rdist (version 6.1.2) to interpolate the output of shell commands into distfiles, borrowing the backquote syntax from UNIX shells. This enables us to use hostcap (or actually hostnames) in a distfile to dynamically select a set of hosts on which a particular file is to be updated. For example, when changing the (common) root password on all our centrally administered Suns except on a Solaris-2-machine named newton and except on the machines belonging to the ``Biglab'', we use a distfile like this:

   # Distfile for /etc/passwd and /etc/shadow.
   # We use shadow passwords under Solaris 2, but not under SunOS 4.

   SPARC_SOL_2=
       `hostnames admin==1 arch==sparc os=~solaris2 \
                  name!=newton org!=+biglab`

   SPARC_SUNOS_4=
       `hostnames admin==1 os=~sunos4 org!=+biglab`

   /home/platform/sparc-solaris2/etc/shadow -> ${SPARC_SOL_2}
       install /etc/shadow;

   /home/platform/sparc-sunos4/etc/passwd -> ${SPARC_SUNOS_4}
       install /etc/passwd;

Hostcap and remote execution: Along the same lines, we have a shell script named rdo that is using rdist and hostcap to run a command on a set of hosts determined by a sequence of hostcap relational expressions:

   rdo arch==sparc org!=+biglab -- grep root /etc/passwd

The arguments to rdo before the ``--'' delimiter are passed to hostcap, the remaining arguments make up the command to be executed on each host.

Top: hostcap overview  Prev: Capabilities...  Next: Life with hostcap