summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--lib/facter/ipmess.rb43
-rw-r--r--lib/facter/util/ip.rb84
-rw-r--r--spec/unit/data/linux_ifconfig_all_with_single_interface18
-rw-r--r--spec/unit/data/solaris_ifconfig_single_interface3
-rw-r--r--spec/unit/util/ip.rb40
5 files changed, 105 insertions, 83 deletions
diff --git a/lib/facter/ipmess.rb b/lib/facter/ipmess.rb
index 215d557..ce6e420 100644
--- a/lib/facter/ipmess.rb
+++ b/lib/facter/ipmess.rb
@@ -15,61 +15,32 @@ Facter.add(:interfaces) do
end
case Facter.value(:kernel)
- when 'SunOS', 'Linux'
+ when 'SunOS', 'Linux', 'OpenBSD', 'NetBSD', 'FreeBSD'
Facter::IPAddress.get_interfaces.each do |interface|
mi = interface.gsub(':', '_')
Facter.add("ipaddress_" + mi) do
- confine :kernel => [ :sunos, :linux ]
+ confine :kernel => [ :sunos, :freebsd, :openbsd, :netbsd, :linux ]
setcode do
label = 'ipaddress'
- Facter::IPAddress.get_interface_value_nonbsd(interface, label)
+ Facter::IPAddress.get_interface_value(interface, label)
end
end
Facter.add("macaddress_" + mi) do
- confine :kernel => [ :sunos, :linux ]
+ confine :kernel => [ :sunos, :freebsd, :openbsd, :netbsd, :linux ]
setcode do
label = 'macaddress'
- Facter::IPAddress.get_interface_value_nonbsd(interface, label)
+ Facter::IPAddress.get_interface_value(interface, label)
end
end
Facter.add("netmask_" + mi) do
- confine :kernel => [ :sunos, :linux ]
+ confine :kernel => [ :sunos, :freebsd, :openbsd, :netbsd, :linux ]
setcode do
label = 'netmask'
- Facter::IPAddress.get_interface_value_nonbsd(interface, label)
+ Facter::IPAddress.get_interface_value(interface, label)
end
end
end
-
- when 'OpenBSD', 'NetBSD', 'FreeBSD'
- Facter::IPAddress.get_interfaces.each do |interface|
- mi = interface.gsub(':', '_')
-
- Facter.add("ipaddress_" + mi) do
- confine :kernel => [ :openbsd, :freebsd, :netbsd ]
- setcode do
- label = 'ipaddress'
- Facter::IPAddress.get_interface_value_bsd(interface, label)
- end
- end
-
- Facter.add("netmask_" + mi) do
- confine :kernel => [ :openbsd, :freebsd, :netbsd ]
- setcode do
- label = 'netmask'
- Facter::IPAddress.get_interface_value_bsd(interface, label)
- end
- end
-
- Facter.add("macaddress_" + mi) do
- confine :kernel => [ :openbsd, :freebsd, :netbsd ]
- setcode do
- label = 'macaddress'
- Facter::IPAddress.get_interface_value_bsd(interface, label)
- end
- end
- end
end
diff --git a/lib/facter/util/ip.rb b/lib/facter/util/ip.rb
index 1a0c611..f6efdc2 100644
--- a/lib/facter/util/ip.rb
+++ b/lib/facter/util/ip.rb
@@ -4,13 +4,8 @@ module Facter::IPAddress
int = nil
- case Facter.value(:kernel)
- when 'Linux', 'OpenBSD', 'NetBSD', 'FreeBSD'
- output = %x{/sbin/ifconfig -a}
- when 'SunOS'
- output = %x{/usr/sbin/ifconfig -a}
- end
-
+ output = Facter::IPAddress.get_all_interface_output()
+
# We get lots of warnings on platforms that don't get an output
# made.
if output
@@ -20,24 +15,43 @@ module Facter::IPAddress
end
end
+
+ def self.get_all_interface_output
+ case Facter.value(:kernel)
+ when 'Linux', 'OpenBSD', 'NetBSD', 'FreeBSD'
+ output = %x{/sbin/ifconfig -a}
+ when 'SunOS'
+ output = %x{/usr/sbin/ifconfig -a}
+ end
+ output
+ end
+
+ def self.get_single_interface_output(interface)
+ output = ""
+ case Facter.value(:kernel)
+ when 'Linux', 'OpenBSD', 'NetBSD', 'FreeBSD'
+ output = %x{/sbin/ifconfig #{interface}}
+ when 'SunOS'
+ output = %x{/usr/sbin/ifconfig #{interface}}
+ end
+ output
+ end
+
+
+ def self.get_interface_value(interface, label)
- def self.get_interface_value_nonbsd(interface, label)
-
- tmp1 = nil
+ tmp1 = []
case Facter.value(:kernel)
when 'Linux'
- output_int = %x{/sbin/ifconfig #{interface}}
addr = /inet addr:([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/
mac = /(?:ether|HWaddr)\s+(\w{1,2}:\w{1,2}:\w{1,2}:\w{1,2}:\w{1,2}:\w{1,2})/
mask = /Mask:([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/
when 'OpenBSD', 'NetBSD', 'FreeBSD'
- output_int = %x{/sbin/ifconfig #{interface}}
addr = /inet\s+([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/
mac = /(?:ether|lladdr)\s+(\w\w:\w\w:\w\w:\w\w:\w\w:\w\w)/
mask = /netmask\s+(\w{10})/
when 'SunOS'
- output_int = %x{/usr/sbin/ifconfig #{interface}}
addr = /inet\s+([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/
mac = /(?:ether|lladdr)\s+(\w?\w:\w?\w:\w?\w:\w?\w:\w?\w:\w?\w)/
mask = /netmask\s+(\w{8})/
@@ -51,48 +65,24 @@ module Facter::IPAddress
when 'netmask'
regex = mask
end
+
+ output_int = get_single_interface_output(interface)
if interface != "lo" && interface != "lo0"
output_int.each { |s|
- tmp1 = $1 if s =~ regex
+ if s =~ regex
+ value = $1
+ if label == 'netmask' && Facter.value(:kernel) == "SunOS"
+ value = value.scan(/../).collect do |byte| byte.to_i(16) end.join('.')
+ end
+ tmp1.push(value)
+ end
}
end
if tmp1
- value = tmp1
+ value = tmp1.shift
end
end
-
- def self.get_interface_value_bsd(interface, label)
-
- tmp1 = []
-
- int_hash = {}
- output_int = %x{/sbin/ifconfig #{interface}}
- addr = /inet\s+([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/
- mac = /(?:ether|lladdr)\s+(\w\w:\w\w:\w\w:\w\w:\w\w:\w\w)/
- mask = /netmask\s+(\w{10})/
-
- case label
- when 'ipaddress'
- regex = addr
- when 'macaddress'
- regex = mac
- when 'netmask'
- regex = mask
- end
-
- if interface != "lo" && interface != "lo0"
- output_int.each { |s|
- tmp1.push($1) if s =~ regex
- }
- end
-
- if tmp1
- value = tmp1.shift
- end
-
- end
end
-
diff --git a/spec/unit/data/linux_ifconfig_all_with_single_interface b/spec/unit/data/linux_ifconfig_all_with_single_interface
new file mode 100644
index 0000000..e063b77
--- /dev/null
+++ b/spec/unit/data/linux_ifconfig_all_with_single_interface
@@ -0,0 +1,18 @@
+eth0 Link encap:Ethernet HWaddr 00:0c:29:52:15:e9
+ inet addr:172.16.15.133 Bcast:172.16.15.255 Mask:255.255.255.0
+ inet6 addr: fe80::20c:29ff:fe52:15e9/64 Scope:Link
+ UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
+ RX packets:173 errors:173 dropped:0 overruns:0 frame:0
+ TX packets:208 errors:0 dropped:0 overruns:0 carrier:0
+ collisions:0 txqueuelen:1000
+ RX bytes:40970 (40.0 KB) TX bytes:24760 (24.1 KB)
+ Interrupt:16 Base address:0x2024
+
+lo Link encap:Local Loopback
+ inet addr:127.0.0.1 Mask:255.0.0.0
+ inet6 addr: ::1/128 Scope:Host
+ UP LOOPBACK RUNNING MTU:16436 Metric:1
+ RX packets:1630 errors:0 dropped:0 overruns:0 frame:0
+ TX packets:1630 errors:0 dropped:0 overruns:0 carrier:0
+ collisions:0 txqueuelen:0
+ RX bytes:81500 (79.5 KB) TX bytes:81500 (79.5 KB) \ No newline at end of file
diff --git a/spec/unit/data/solaris_ifconfig_single_interface b/spec/unit/data/solaris_ifconfig_single_interface
new file mode 100644
index 0000000..7a227aa
--- /dev/null
+++ b/spec/unit/data/solaris_ifconfig_single_interface
@@ -0,0 +1,3 @@
+e1000g0: flags=201004843<UP,BROADCAST,RUNNING,MULTICAST,DHCP,IPv4,CoS> mtu 1500 index 2
+ inet 172.16.15.138 netmask ffffff00 broadcast 172.16.15.255
+ ether 0:c:29:c1:70:2a \ No newline at end of file
diff --git a/spec/unit/util/ip.rb b/spec/unit/util/ip.rb
new file mode 100644
index 0000000..e27531e
--- /dev/null
+++ b/spec/unit/util/ip.rb
@@ -0,0 +1,40 @@
+#!/usr/bin/env ruby
+
+require File.dirname(__FILE__) + '/../../spec_helper'
+
+require 'facter/util/ip'
+
+describe Facter::IPAddress do
+
+ it "should return a list of interfaces" do
+ Facter::IPAddress.should respond_to(:get_interfaces)
+ end
+
+ it "should return an empty list of interfaces on an unknown kernel" do
+ Facter.stubs(:value).returns("UnknownKernel")
+ Facter::IPAddress.get_interfaces().should == []
+ end
+
+ it "should return a list with a single interface on Linux with a single interface" do
+ sample_output_file = File.dirname(__FILE__) + '/../data/linux_ifconfig_all_with_single_interface'
+ linux_ifconfig = File.new(sample_output_file).read()
+ Facter::IPAddress.stubs(:get_all_interface_output).returns(linux_ifconfig)
+ Facter::IPAddress.get_interfaces().should == ["eth0"]
+ end
+
+ it "should return a value for a specific interface" do
+ Facter::IPAddress.should respond_to(:get_interface_value)
+ end
+
+ it "should return a human readable netmask on Solaris" do
+ sample_output_file = File.dirname(__FILE__) + "/../data/solaris_ifconfig_single_interface"
+ solaris_ifconfig_interface = File.new(sample_output_file).read()
+
+ Facter::IPAddress.expects(:get_single_interface_output).with("e1000g0").returns(solaris_ifconfig_interface)
+ Facter.stubs(:value).with(:kernel).returns("SunOS")
+
+ Facter::IPAddress.get_interface_value("e1000g0", "netmask").should == "255.255.255.0"
+ end
+
+end
+