blob: dd0d4183eb0649c866392adef3d22f77704d7a1c (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
|
Facter.add(:ipaddress) do
confine :kernel => :linux
setcode do
ip = nil
output = %x{/sbin/ifconfig}
output.split(/^\S/).each { |str|
if str =~ /inet addr:([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/
tmp = $1
unless tmp =~ /^127\./
ip = tmp
break
end
end
}
ip
end
end
Facter.add(:ipaddress) do
confine :kernel => %w{FreeBSD OpenBSD}
setcode do
ip = nil
output = %x{/sbin/ifconfig}
output.split(/^\S/).each { |str|
if str =~ /inet ([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/
tmp = $1
unless tmp =~ /^127\./
ip = tmp
break
end
end
}
ip
end
end
Facter.add(:ipaddress) do
confine :kernel => %w{NetBSD SunOS}
setcode do
ip = nil
output = %x{/sbin/ifconfig -a}
output.split(/^\S/).each { |str|
if str =~ /inet ([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/
tmp = $1
unless tmp =~ /^127\./
ip = tmp
break
end
end
}
ip
end
end
Facter.add(:ipaddress) do
confine :kernel => %w{darwin}
setcode do
ip = nil
iface = ""
output = %x{/usr/sbin/netstat -rn}
if output =~ /^default\s*\S*\s*\S*\s*\S*\s*\S*\s*(\S*).*/
iface = $1
else
warn "Could not find a default route. Using first non-loopback interface"
end
if(iface != "")
output = %x{/sbin/ifconfig #{iface}}
else
output = %x{/sbin/ifconfig}
end
output.split(/^\S/).each { |str|
if str =~ /inet ([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/
tmp = $1
unless tmp =~ /^127\./
ip = tmp
break
end
end
}
ip
end
end
Facter.add(:ipaddress) do
confine :kernel => %w{AIX}
setcode do
ip = nil
output = %x{/usr/sbin/ifconfig -a}
output.split(/^\S/).each { |str|
if str =~ /inet ([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/
tmp = $1
unless tmp =~ /^127\./
ip = tmp
break
end
end
}
ip
end
end
Facter.add(:ipaddress) do
confine :kernel => %w{windows}
setcode do
ip = nil
output = %x{ipconfig}
output.split(/^\S/).each { |str|
if str =~ /IP Address.*: ([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)/
tmp = $1
unless tmp =~ /^127\./
ip = tmp
break
end
end
}
ip
end
end
Facter.add(:ipaddress, :ldapname => "iphostnumber", :timeout => 2) do
setcode do
require 'resolv'
begin
if hostname = Facter.value(:hostname)
ip = Resolv.getaddress(hostname)
unless ip == "127.0.0.1"
ip
end
else
nil
end
rescue Resolv::ResolvError
nil
rescue NoMethodError # i think this is a bug in resolv.rb?
nil
end
end
end
Facter.add(:ipaddress, :timeout => 2) do
setcode do
if hostname = Facter.value(:hostname)
# we need Hostname to exist for this to work
host = nil
if host = Facter::Util::Resolution.exec("host #{hostname}")
list = host.chomp.split(/\s/)
if defined? list[-1] and
list[-1] =~ /[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/
list[-1]
end
else
nil
end
else
nil
end
end
end
|