Class: Inspec::Resources::NetworkInterface

Inherits:
Object
  • Object
show all
Defined in:
lib/inspec/resources/interface.rb

Instance Method Summary collapse

Constructor Details

#initialize(iface) ⇒ NetworkInterface

Returns a new instance of NetworkInterface.



21
22
23
# File 'lib/inspec/resources/interface.rb', line 21

def initialize(iface)
  @iface = iface
end

Instance Method Details

#exists?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/inspec/resources/interface.rb', line 25

def exists?
  !!(interface_info && interface_info[:name])
end

#ipv4_addressObject



50
51
52
# File 'lib/inspec/resources/interface.rb', line 50

def ipv4_address
  ipv4_addresses.first
end

#ipv4_address?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/inspec/resources/interface.rb', line 42

def ipv4_address?
  ipv4_addresses && !ipv4_addresses.empty?
end

#ipv4_addressesObject



54
55
56
# File 'lib/inspec/resources/interface.rb', line 54

def ipv4_addresses
  ipv4_cidrs.map { |i| i.split("/")[0] }
end

#ipv4_addresses_netmaskObject



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/inspec/resources/interface.rb', line 66

def ipv4_addresses_netmask
  ipv4_cidrs.map { |i| i.split("/") }.map do |addr, netlen|
    binmask = "#{"1" * netlen.to_i}#{"0" * (32 - netlen.to_i)}".to_i(2)
    netmask = []
    (1..4).each do |_byte|
      netmask.unshift(binmask & 255)
      binmask = binmask >> 8
    end
    "#{addr}/#{netmask.join(".")}"
  end
end

#ipv4_cidrsObject



78
79
80
# File 'lib/inspec/resources/interface.rb', line 78

def ipv4_cidrs
  interface_info && Array(interface_info[:ipv4_addresses])
end

#ipv6_addressObject



58
59
60
# File 'lib/inspec/resources/interface.rb', line 58

def ipv6_address
  ipv6_addresses.first
end

#ipv6_address?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/inspec/resources/interface.rb', line 46

def ipv6_address?
  ipv6_addresses && !ipv6_addresses.empty?
end

#ipv6_addressesObject



62
63
64
# File 'lib/inspec/resources/interface.rb', line 62

def ipv6_addresses
  ipv6_cidrs.map { |i| i.split("/")[0] }
end

#ipv6_cidrsObject



82
83
84
# File 'lib/inspec/resources/interface.rb', line 82

def ipv6_cidrs
  interface_info && Array(interface_info[:ipv6_addresses])
end

#nameObject



33
34
35
# File 'lib/inspec/resources/interface.rb', line 33

def name
  interface_info[:name]
end

#resource_idObject



86
87
88
# File 'lib/inspec/resources/interface.rb', line 86

def resource_id
  @iface || "Interface"
end

#speedObject

returns link speed in Mbits/sec



38
39
40
# File 'lib/inspec/resources/interface.rb', line 38

def speed
  interface_info && interface_info[:speed]
end

#to_sObject



90
91
92
# File 'lib/inspec/resources/interface.rb', line 90

def to_s
  "Interface #{@iface}"
end

#up?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/inspec/resources/interface.rb', line 29

def up?
  !!(interface_info && interface_info[:up])
end