Output
I was needing to get some precise network data :
ipaddress_dummy0 => 192.168.44.1 ipaddress_eth0 => 192.168.2.24 ipaddress_eth0_0 => 192.168.2.25 macaddress_dummy0 => F2:8D:06:55:50:99 macaddress_eth0 => 00:8D:06:55:50:99 macaddress_eth0_0 => 00:8D:06:55:50:99
Code
#
# ipmess.rb
# Try to get additional Facts about the machine's network interfaces on Linux
#
# Original concept Copyright (C) 2007 psychedelys <psychedelys@gmail.com>
# Update and *BSD support (C) 2007 James Turnbull <james@lovedthanlost.net>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation (version 2 of the License)
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston MA 02110-1301 USA
#
if Facter.kernel == "Linux"
output = %x{/sbin/ifconfig -a}
int = nil
output.scan(/^(\w+)(\d+)(:\d+)?/) { |str|
output_int = %x{/sbin/ifconfig #{str}}
int = "#{str}"
tmp1 = nil
tmp2 = nil
test = {}
output_int.each { |s|
int = "#{str}"
tmp1 = $1 if s =~ /inet addr:([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/
tmp2 = $1 if s =~ /(?:ether|HWaddr) (\w{1,2}:\w{1,2}:\w{1,2}:\w{1,2}:\w{1,2}:\w{1,2})/
if tmp1 != nil && tmp2 != nil && int != "lo"
int.sub!(/:/, '_')
test["ipaddress_" + int] = tmp1
test["macaddress_" + int] = tmp2
int = nil
tmp1 = nil
tmp2 = nil
end
}
test.each{|name,fact|
Facter.add(name) do
confine :kernel => :linux
setcode do
fact
end
end
}
}
end
if Facter.kernel == "FreeBSD" || Facter.kernel == "OpenBSD" || Facter.kernel == "NetBSD"
output = %x{/sbin/ifconfig -a}
int = nil
output.scan(/^(\w+)(\d+):/) { |str|
output_int = %x{/sbin/ifconfig #{str}}
int = "#{str}"
tmp1 = nil
tmp2 = nil
test = {}
output_int.each { |s|
int = "#{str}"
tmp1 = $1 if s =~ /inet ([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/
tmp2 = $1 if s =~ /(?:ether|lladdr)\s+(\w\w:\w\w:\w\w:\w\w:\w\w:\w\w)/
if tmp1 != nil && tmp2 != nil && int != "lo"
test["ipaddress_" + int] = tmp1
test["macaddress_" + int] = tmp2
int = nil
tmp1 = nil
tmp2 = nil
end
}
test.each{|name,fact|
Facter.add(name) do
confine :kernel => :linux
setcode do
fact
end
end
}
}
end
Discussion
A modified version of this recipe is contained in Facter versions 1.3.8 and later. The latest code on git repo fix the colon issue, but until it is in a release this code can be used as a workaround.