Skip to content

Commit

Permalink
Merge pull request #829 from MikaelSmith/imp/master/FACT-770-improve-…
Browse files Browse the repository at this point in the history
…network-fact-AIX

(FACT-770) Networking improvements for AIX
  • Loading branch information
Michael Smith committed Dec 18, 2014
2 parents 9697ca7 + 6f14a6d commit cef2809
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 15 deletions.
22 changes: 10 additions & 12 deletions lib/facter/ipaddress.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@
ip = nil
output = Facter::Util::IP.exec_ifconfig

output.split(/^\S/).each { |str|
output.split(/^\S/).each do |str|
if str =~ /inet ([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/
tmp = $1
unless tmp =~ /^127\./
ip = tmp
break
end
end
}
end

ip
end
Expand All @@ -69,15 +69,15 @@
ip = nil
output = Facter::Util::IP.exec_ifconfig(["-a"])

output.split(/^\S/).each { |str|
output.split(/^\S/).each do |str|
if str =~ /inet ([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/
tmp = $1
unless tmp =~ /^127\./ or tmp == "0.0.0.0"
ip = tmp
break
end
end
}
end

ip
end
Expand All @@ -87,17 +87,15 @@
confine :kernel => %w{AIX}
setcode do
ip = nil
output = Facter::Util::IP.exec_ifconfig(["-a"])

output.split(/^\S/).each { |str|
default_interface = Facter::Util::IP.exec_netstat(["-rn | grep default | awk '{ print $6 }'"])
output = Facter::Util::IP.exec_ifconfig([default_interface])

output.split(/^\S/).each do |str|
if str =~ /inet ([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/
tmp = $1
unless tmp =~ /^127\./
ip = tmp
break
end
ip = $1
end
}
end

ip
end
Expand Down
3 changes: 2 additions & 1 deletion lib/facter/macaddress.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@
setcode do
ether = []
ip = nil
output = Facter::Util::IP.exec_ifconfig(["-a"])
default_interface = Facter::Util::IP.exec_netstat(["-rn | grep default | awk '{ print $6 }'"])
output = Facter::Util::IP.exec_ifconfig([default_interface])
output.each_line do |str|
if str =~ /([a-z]+\d+): flags=/
devname = $1
Expand Down
59 changes: 57 additions & 2 deletions lib/facter/util/ip.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ module Facter::Util::IP
:macaddress => /(\w{1,2}:\w{1,2}:\w{1,2}:\w{1,2}:\w{1,2}:\w{1,2})/,
:netmask => /.*\s+netmask (\S+)\s.*/
},
:aix => {
:ipaddress => /inet\s+([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/,
:ipaddress6 => /inet6 ((?![fe80|::1])(?>[0-9,a-f,A-F]*\:{1,2})+[0-9,a-f,A-F]{0,4})/,
:netmask => /netmask\s+0x(\w{8})/,
:mtu => /mtu\s+(\d+)/,
:macaddress => /^Hardware\sAddress:\s(\w{1,2}:\w{1,2}:\w{1,2}:\w{1,2}:\w{1,2}:\w{1,2})/
},
:windows => {}
}

Expand All @@ -40,7 +47,7 @@ def self.alphafy(interface)
end

def self.convert_from_hex?(kernel)
kernels_to_convert = [:sunos, :openbsd, :netbsd, :freebsd, :darwin, :"hp-ux", :"gnu/kfreebsd", :dragonfly]
kernels_to_convert = [:sunos, :openbsd, :netbsd, :freebsd, :darwin, :"hp-ux", :"gnu/kfreebsd", :dragonfly, :aix]
kernels_to_convert.include?(kernel)
end

Expand Down Expand Up @@ -73,7 +80,7 @@ def self.get_interfaces

def self.get_all_interface_output
case Facter.value(:kernel)
when 'Linux', 'OpenBSD', 'NetBSD', 'FreeBSD', 'Darwin', 'GNU/kFreeBSD', 'DragonFly'
when 'Linux', 'OpenBSD', 'NetBSD', 'FreeBSD', 'Darwin', 'GNU/kFreeBSD', 'DragonFly', 'AIX'
output = Facter::Util::IP.exec_ifconfig(["-a","2>/dev/null"])
when 'SunOS'
output = Facter::Util::IP.exec_ifconfig(["-a"])
Expand Down Expand Up @@ -106,6 +113,23 @@ def self.get_ifconfig
common_paths=["/bin/ifconfig","/sbin/ifconfig","/usr/sbin/ifconfig"]
common_paths.select{|path| File.executable?(path)}.first
end

##
# exec_netstat uses the netstat command
#
# @return [String] the output of `netstat #{arguments} 2>/dev/null` or nil
def self.exec_netstat(additional_arguments=[])
Facter::Core::Execution.exec("#{self.get_netstat} #{additional_arguments.join(' ')}")
end
##
# get_netstat looks up the netstat binary
#
# @return [String] path to the netstat binary
def self.get_netstat
common_paths=["/bin/netstat","/sbin/netstat","/usr/sbin/netstat","/usr/bin/netstat"]
common_paths.select{|path| File.executable?(path)}.first
end

##
# hpux_netstat_in is a delegate method that allows us to stub netstat -in
# without stubbing exec.
Expand Down Expand Up @@ -151,6 +175,9 @@ def self.get_single_interface_output(interface)
hpux_lanscan.scan(/(\dx\S+).*UP\s+(\w+\d+)/).each {|i| mac = i[0] if i.include?(interface) }
mac = mac.sub(/0x(\S+)/,'\1').scan(/../).join(":")
output = ifc + "\n" + mac
when 'AIX'
output = Facter::Util::IP.ifconfig_interface(interface) + "\n" + aix_get_mtu(interface) + "\n" + aix_get_macadress(interface)
output
end
output
end
Expand All @@ -163,6 +190,34 @@ def self.hpux_lanscan
Facter::Core::Execution.exec("/usr/sbin/lanscan")
end

def self.aix_lsattr_interface(interface)
Facter::Core::Execution.exec("/usr/sbin/lsattr -El #{interface}")
end

def self.aix_entstat_interface(interface)
if interface[0..1] != "lo"
Facter::Core::Execution.exec("/usr/bin/entstat #{interface}")
else
return ""
end
end

def self.aix_get_mtu(interface)
aix_lsattr_interface(interface).each_line do |s|
if s =~ /mtu\s+(\d+)/
return s
end
end
end

def self.aix_get_macadress(interface)
aix_entstat_interface(interface).each_line do |s|
if s =~ /^Hardware\sAddress:\s(\w{1,2}:\w{1,2}:\w{1,2}:\w{1,2}:\w{1,2}:\w{1,2})/
return s
end
end
end

def self.get_output_for_interface_and_label(interface, label)
return get_single_interface_output(interface) unless Facter.value(:kernel) == 'windows'

Expand Down

0 comments on commit cef2809

Please sign in to comment.