Class: MacOS::IfConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/MacOS/IfConfig.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(interface: nil, mac_address: nil, ipv4_address: nil, netmask: nil, broadcast_address: nil, status: nil) ⇒ IfConfig

Returns a new instance of IfConfig.



117
118
119
120
121
122
# File 'lib/MacOS/IfConfig.rb', line 117

def initialize(interface: nil, mac_address: nil, ipv4_address: nil, netmask: nil, broadcast_address:nil, status: nil)
  @interface = interface
  @mac_address = mac_address
  @ipv4_address, @netmask, @broadcast_address = ipv4_address, netmask, broadcast_address
  @status = status
end

Instance Attribute Details

#broadcast_addressObject

Returns the value of attribute broadcast_address.



114
115
116
# File 'lib/MacOS/IfConfig.rb', line 114

def broadcast_address
  @broadcast_address
end

#interfaceObject

class << self



112
113
114
# File 'lib/MacOS/IfConfig.rb', line 112

def interface
  @interface
end

#ipv4_addressObject

Returns the value of attribute ipv4_address.



114
115
116
# File 'lib/MacOS/IfConfig.rb', line 114

def ipv4_address
  @ipv4_address
end

#mac_addressObject

Returns the value of attribute mac_address.



113
114
115
# File 'lib/MacOS/IfConfig.rb', line 113

def mac_address
  @mac_address
end

#netmaskObject

Returns the value of attribute netmask.



114
115
116
# File 'lib/MacOS/IfConfig.rb', line 114

def netmask
  @netmask
end

#statusObject

Returns the value of attribute status.



115
116
117
# File 'lib/MacOS/IfConfig.rb', line 115

def status
  @status
end

Class Method Details

.active_interfacesObject Also known as: active



56
57
58
# File 'lib/MacOS/IfConfig.rb', line 56

def active_interfaces
  all.select(&:active?)
end

.active_interfaces_namesObject



61
62
63
# File 'lib/MacOS/IfConfig.rb', line 61

def active_interfaces_names
  all.select(&:active?).collect(&:interface_name)
end

.find_by_interface(interface) ⇒ Object



92
93
94
# File 'lib/MacOS/IfConfig.rb', line 92

def find_by_interface(interface)
  all.find{|ifconfig| ifconfig.interface == interface}
end

.find_by_ipv4_address(ipv4_address) ⇒ Object



100
101
102
# File 'lib/MacOS/IfConfig.rb', line 100

def find_by_ipv4_address(ipv4_address)
  all.find{|ifconfig| ifconfig.ipv4_address == ipv4_address}
end

.find_by_mac_address(mac_address) ⇒ Object



96
97
98
# File 'lib/MacOS/IfConfig.rb', line 96

def find_by_mac_address(mac_address)
  all.find{|ifconfig| ifconfig.mac_address == mac_address}
end

.inactive_interfacesObject Also known as: inactive



65
66
67
# File 'lib/MacOS/IfConfig.rb', line 65

def inactive_interfaces
  all.select(&:inactive?)
end

.inactive_interfaces_namesObject



70
71
72
# File 'lib/MacOS/IfConfig.rb', line 70

def inactive_interfaces_names
  all.select(&:inactive?).collect(&:interface_name)
end

.interface_namesObject



52
53
54
# File 'lib/MacOS/IfConfig.rb', line 52

def interface_names
  all.collect(&:interface_name)
end

.ipv4_addressesObject



88
89
90
# File 'lib/MacOS/IfConfig.rb', line 88

def ipv4_addresses
  all.collect(&:ipv4_address).compact
end

.mac_addressesObject Also known as: ethernet_addresses



83
84
85
# File 'lib/MacOS/IfConfig.rb', line 83

def mac_addresses
  all.collect(&:mac_address).compact
end

.parse(output = nil) ⇒ Object Also known as: all, interfaces



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
# File 'lib/MacOS/IfConfig.rb', line 20

def parse(output = nil)
  output ||= self.output
  @ifconfigs = []
  interface = nil
  output.each_line do |line|
    if line =~ /^\t/
      case line
      when /ether/
        @ifconfig.mac_address = line.capture(/ *ether (.+) $/)
      when /inet /
        if line.match(/broadcast/)
          @ifconfig.ipv4_address, @ifconfig.netmask, @ifconfig.broadcast_address = line.captures(/ *inet (\d+(?:\.\d+){3}) netmask (.+) broadcast (.+)$/)
        elsif line.match(/-->/)
          ipv4_addresses, @ifconfig.netmask = line.captures(/ *inet (.+) netmask (.+) $/)
          @ifconfig.ipv4_address = ipv4_addresses.split(' --> ').first
        else
          @ifconfig.ipv4_address, @ifconfig.netmask = line.captures(/ *inet (\d+(?:\.\d+){3}) netmask (.+) $/)
        end
      when /status\:/
        @ifconfig.status = line.capture(/ *status: (.+)$/)
      end
    else
      @ifconfigs << @ifconfig if @ifconfig
      @ifconfig = new
      @ifconfig.interface, _rest_of_line = line.captures(/^(.+?): (.+)$/)
    end
  end
  @ifconfigs << @ifconfig
end

.up_interfacesObject Also known as: up



74
75
76
# File 'lib/MacOS/IfConfig.rb', line 74

def up_interfaces
  all.select(&:up?)
end

.up_interfaces_namesObject



79
80
81
# File 'lib/MacOS/IfConfig.rb', line 79

def up_interfaces_names
  all.select(&:up?).collect(&:interface_name)
end

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


133
134
135
# File 'lib/MacOS/IfConfig.rb', line 133

def active?
  @status == 'active'
end

#ethernet_addressObject



129
130
131
# File 'lib/MacOS/IfConfig.rb', line 129

def ethernet_address
  @mac_address
end

#inactive?Boolean

Returns:

  • (Boolean)


137
138
139
# File 'lib/MacOS/IfConfig.rb', line 137

def inactive?
  @status == 'inactive'
end

#interface_nameObject Also known as: name



124
125
126
# File 'lib/MacOS/IfConfig.rb', line 124

def interface_name
  @interface
end

#up?Boolean

Returns:

  • (Boolean)


141
142
143
# File 'lib/MacOS/IfConfig.rb', line 141

def up?
  active? || @ipv4_address
end