diff options
author | James Turnbull <james@lovedthanlost.net> | 2009-01-27 19:37:54 +1100 |
---|---|---|
committer | James Turnbull <james@lovedthanlost.net> | 2009-01-27 19:37:54 +1100 |
commit | fab9d1c63f0a3671dc5f8fa0233823a59f1bce06 (patch) | |
tree | e7423bf58300544d2b2859cbbee9e93d47fda2ef | |
parent | da52e30971ebb6935c64777e4a91cb2cbb066db9 (diff) | |
download | facter-fab9d1c63f0a3671dc5f8fa0233823a59f1bce06.tar.gz facter-fab9d1c63f0a3671dc5f8fa0233823a59f1bce06.tar.xz facter-fab9d1c63f0a3671dc5f8fa0233823a59f1bce06.zip |
Added network fact
-rw-r--r-- | CHANGELOG | 2 | ||||
-rw-r--r-- | lib/facter/interfaces.rb | 3 | ||||
-rw-r--r-- | lib/facter/network.rb | 10 | ||||
-rw-r--r-- | lib/facter/util/ip.rb | 13 | ||||
-rw-r--r-- | spec/unit/data/darwin_ifconfig_single_interface | 6 |
5 files changed, 32 insertions, 2 deletions
@@ -1,4 +1,6 @@ 1.5.3: + Added network fact + Fixed #1870 - Format all subnet masks as human-readable Added uptime facts diff --git a/lib/facter/interfaces.rb b/lib/facter/interfaces.rb index bf0cc6e..1239215 100644 --- a/lib/facter/interfaces.rb +++ b/lib/facter/interfaces.rb @@ -18,13 +18,12 @@ Facter.add(:interfaces) do end Facter::Util::IP.get_interfaces.each do |interface| - mi = Facter::Util::IP.alphafy(interface) # Make a fact for each detail of each interface. Yay. # There's no point in confining these facts, since we wouldn't be able to create # them if we weren't running on a supported platform. %w{ipaddress macaddress netmask}.each do |label| - Facter.add(label + "_" + mi) do + Facter.add(label + "_" + Facter::Util::IP.alphafy(interface)) do setcode do Facter::Util::IP.get_interface_value(interface, label) end diff --git a/lib/facter/network.rb b/lib/facter/network.rb new file mode 100644 index 0000000..513282a --- /dev/null +++ b/lib/facter/network.rb @@ -0,0 +1,10 @@ +require 'facter/util/ip' + +Facter::Util::IP.get_interfaces.each do |interface| + + Facter.add("network_" + Facter::Util::IP.alphafy(interface)) do + setcode do + Facter::Util::IP.get_network_value(interface) + end + end +end diff --git a/lib/facter/util/ip.rb b/lib/facter/util/ip.rb index a7f0a85..6c01033 100644 --- a/lib/facter/util/ip.rb +++ b/lib/facter/util/ip.rb @@ -140,4 +140,17 @@ module Facter::Util::IP end end end + + def self.get_network_value(interface) + require 'ipaddr' + + ipaddress = get_interface_value(interface, "ipaddress") + netmask = get_interface_value(interface, "netmask") + + if ipaddress && netmask + ip = IPAddr.new(ipaddress, Socket::AF_INET) + subnet = IPAddr.new(netmask, Socket::AF_INET) + network = ip.mask(subnet.to_s) + end + end end diff --git a/spec/unit/data/darwin_ifconfig_single_interface b/spec/unit/data/darwin_ifconfig_single_interface new file mode 100644 index 0000000..d1e8bd3 --- /dev/null +++ b/spec/unit/data/darwin_ifconfig_single_interface @@ -0,0 +1,6 @@ +en1: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500 + inet6 fe80::21c:b3ff:febe:81c9%en1 prefixlen 64 scopeid 0x6 + inet 10.0.0.101 netmask 0xffffff00 broadcast 10.0.0.255 + ether 00:1c:b3:be:81:c9 + media: autoselect status: active + supported media: autoselect |