Added facts arp (like the ipaddress etc) facts Added facts arp_interfacename
Signed-off-by: James Turnbull <[email protected]> --- Local-branch: tickets/master/2346 lib/facter/arp.rb | 22 ++++++++++++++++++++++ lib/facter/util/ip.rb | 11 +++++++++-- spec/unit/util/ip_spec.rb | 23 +++++++++++++++-------- 3 files changed, 46 insertions(+), 10 deletions(-) create mode 100644 lib/facter/arp.rb diff --git a/lib/facter/arp.rb b/lib/facter/arp.rb new file mode 100644 index 0000000..f4b7709 --- /dev/null +++ b/lib/facter/arp.rb @@ -0,0 +1,22 @@ +require 'facter/util/ip' + +Facter.add(:arp) do + confine :kernel => :linux + setcode do + arp = [] + output = %x{/usr/sbin/arp -a} + output.each_line do |s| + arp.push($1) if s =~ /^\S+\s\S+\s\S+\s(\S+)\s\S+\s\S+\s\S+$/ + end + arp[0] + end +end + +Facter::Util::IP.get_interfaces.each do |interface| + Facter.add("arp_" + Facter::Util::IP.alphafy(interface)) do + confine :kernel => :linux + setcode do + Facter::Util::IP.get_arp_value(interface) + end + end +end diff --git a/lib/facter/util/ip.rb b/lib/facter/util/ip.rb index 23eeb9c..8174b4e 100644 --- a/lib/facter/util/ip.rb +++ b/lib/facter/util/ip.rb @@ -158,17 +158,24 @@ 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).to_s end end + + def self.get_arp_value(interface) + arp = Facter::Util::Resolution.exec("arp -en -i #{interface} | sed -e 1d") + if arp =~ /^\S+\s+\w+\s+(\S+)\s+\w\s+\S+$/ + return $1 + end + end end diff --git a/spec/unit/util/ip_spec.rb b/spec/unit/util/ip_spec.rb index ceceb3f..b337fe1 100755 --- a/spec/unit/util/ip_spec.rb +++ b/spec/unit/util/ip_spec.rb @@ -46,7 +46,7 @@ describe Facter::Util::IP do hpux_netstat = File.new(sample_output_file).read() Facter::Util::IP.stubs(:get_all_interface_output).returns(hpux_netstat) Facter::Util::IP.get_interfaces().should == ["lan1", "lan0", "lo0"] - end + end it "should return a list of six interfaces on a GNU/kFreeBSD with six interfaces" do sample_output_file = File.dirname(__FILE__) + '/../data/debian_kfreebsd_ifconfig' @@ -102,7 +102,7 @@ describe Facter::Util::IP do Facter.stubs(:value).with(:kernel).returns("HP-UX") Facter::Util::IP.get_interface_value("lan0", "ipaddress").should == "168.24.80.71" - end + end it "should return macaddress information for HP-UX" do sample_output_file = File.dirname(__FILE__) + "/../data/hpux_ifconfig_single_interface" @@ -112,7 +112,7 @@ describe Facter::Util::IP do Facter.stubs(:value).with(:kernel).returns("HP-UX") Facter::Util::IP.get_interface_value("lan0", "macaddress").should == "00:13:21:BD:9C:B7" - end + end it "should return macaddress with leading zeros stripped off for GNU/kFreeBSD" do sample_output_file = File.dirname(__FILE__) + "/../data/debian_kfreebsd_ifconfig" @@ -132,7 +132,7 @@ describe Facter::Util::IP do Facter.stubs(:value).with(:kernel).returns("HP-UX") Facter::Util::IP.get_interface_value("lan0", "netmask").should == "255.255.255.0" - end + end it "should return calculated network information for HP-UX" do sample_output_file = File.dirname(__FILE__) + "/../data/hpux_ifconfig_single_interface" @@ -142,7 +142,7 @@ describe Facter::Util::IP do Facter.stubs(:value).with(:kernel).returns("HP-UX") Facter::Util::IP.get_network_value("lan0").should == "168.24.80.0" - end + end it "should return interface information for FreeBSD supported via an alias" do sample_output_file = File.dirname(__FILE__) + "/../data/6.0-STABLE_FreeBSD_ifconfig" @@ -192,7 +192,7 @@ describe Facter::Util::IP do Facter.stubs(:value).with(:kernel).returns("HP-UX") Facter::Util::IP.get_interface_value("lan0", "netmask").should == "255.255.255.0" - end + end it "should return a human readable netmask on Darwin" do sample_output_file = File.dirname(__FILE__) + "/../data/darwin_ifconfig_single_interface" @@ -204,7 +204,7 @@ describe Facter::Util::IP do Facter::Util::IP.get_interface_value("en1", "netmask").should == "255.255.255.0" end - + it "should return a human readable netmask on GNU/kFreeBSD" do sample_output_file = File.dirname(__FILE__) + "/../data/debian_kfreebsd_ifconfig" @@ -218,7 +218,7 @@ describe Facter::Util::IP do it "should not get bonding master on interface aliases" do Facter.stubs(:value).with(:kernel).returns("Linux") - + Facter::Util::IP.get_bonding_master("eth0:1").should be_nil end @@ -227,4 +227,11 @@ describe Facter::Util::IP do Facter::Util::IP.convert_from_hex?(platform).should == true end end + + it "should return an arp address on Linux" do + Facter.stubs(:value).with(:kernel).returns("Linux") + + Facter::Util::IP.expects(:get_arp_value).with("eth0").returns("00:00:0c:9f:f0:04") + Facter::Util::IP.get_arp_value("eth0").should == "00:00:0c:9f:f0:04" + end end -- 1.7.1 -- You received this message because you are subscribed to the Google Groups "Puppet Developers" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/puppet-dev?hl=en.
