forked from kwilczynski/facter-facts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
default_gateway.rb
80 lines (68 loc) · 2.51 KB
/
default_gateway.rb
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
#
# default_gateway.rb
#
# This fact provides information about the default gateway (or the "route of
# last resort" if you wish) that is available on the system ...
#
require 'facter'
if Facter.value(:kernel) == 'Linux'
# We store information about the default gateway here ...
gateway = ''
gateway_interface = ''
#
# Modern Linux kernels provide "/proc/net/route" in the following format:
#
# Iface Destination Gateway Flags RefCnt Use Metric Mask MTU Window IRTT
# eth0 0000FEA9 00000000 0001 0 0 1000 0000FFFF 0 0 0
# eth0 00006C0A 00000000 0001 0 0 1 0000FFFF 0 0 0
# eth0 00000000 460B6C0A 0003 0 0 0 00000000 0 0 0
#
#
# We utilise rely on "cat" for reading values from entries under "/proc".
# This is due to some problems with IO#read in Ruby and reading content of
# the "proc" file system that was reported more than once in the past ...
#
Facter::Util::Resolution.exec('cat /proc/net/route 2> /dev/null').each_line do |line|
# Remove bloat ...
line.strip!
# Skip header line ...
next if line.match(/^[Ii]face.+/)
# Skip new and empty lines ...
next if line.match(/^(\r\n|\n|\s*)$|^$/)
# Retrieve destination and gateway ...
values = line.split("\t")[0..2]
# Convert values to a pair of bytes ...
interface = values[0]
values.collect! { |i| i.to_a.pack('H*') }
# Add all the bytes together ...
sum = values[1].unpack('C4').inject { |i, j| i + j }
#
# A value (actually, a sum if you wish) of zero will indicate that we have
# the default gateway. Anything else indicates known destination ...
#
unless sum > 0
# A default gateway there? Convert back to Integer ...
gateway = values[2].unpack('C4')
gateway_interface = interface
else
# Skip irrelevant entries ...
next
end
end
# Sometimes we do not have the default route set at all ...
if gateway and not gateway.empty?
Facter.add('default_gateway') do
confine :kernel => :linux
# Reverse from network order ...
setcode { gateway.reverse.join('.') }
end
end
if gateway_interface and not gateway_interface.empty?
Facter.add('default_gateway_interface') do
confine :kernel => :linux
setcode { gateway_interface }
end
end
end
# vim: set ts=2 sw=2 et :
# encoding: utf-8