Skip to main content
  1. Blog/

Detecting NICs that match certain IP addresses

·32 words·1 min·
Diego Zamboni
Author
Diego Zamboni
Another cfengine snippet in case anyone finds it useful. This can be used to determine which NICs in the host have a certain IP address assigned to it, or match any pattern.

# Find all network interfaces that match a certain IP address.
# Time-stamp: <[netif_find.cf] modified by Diego Zamboni on Tuesday, 2010.08.24 at 01:53:03>
body common control
{
bundlesequence => { "test" };
}
bundle agent test
{
vars:
"nics" slist => getindices("sys.ipv4");
# Regex we want to match on the IP address
"ipregex" string => "192\.168\..*";
# This is heavily based on cfengine3 list expansion. The expression is
# evaluated for every value of ${nics}, and if there are any matches,
# the corresponding class is defined, which then triggers the printing
# of the "Matched NIC" message (of course, in a real example, the class
# could be used to trigger some other action).
classes:
"ismatch_${nics}" expression => regcmp("${ipregex}", "${sys.ipv4[${nics}]}");
reports:
linux|windows::
"NICs found: ${nics}";
"Matched NIC: ${nics} (${sys.ipv4[${nics}]})"
ifvarclass => "ismatch_${nics}";
}
view raw netif_find.cf hosted with ❤ by GitHub