Class: NiseBOSHVagrant::NetworkInterface

Inherits:
Object
  • Object
show all
Defined in:
lib/nise-bosh-vagrant/network_interface.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(vbox_name, ip_address, network_mask, gateway) ⇒ NetworkInterface

Returns a new instance of NetworkInterface.



38
39
40
41
42
43
# File 'lib/nise-bosh-vagrant/network_interface.rb', line 38

def initialize(vbox_name, ip_address, network_mask, gateway)
  @vbox_name = vbox_name
  @ip_address = ip_address
  @network_mask = network_mask
  @gateway = gateway
end

Instance Attribute Details

#gatewayObject (readonly)

Returns the value of attribute gateway.



36
37
38
# File 'lib/nise-bosh-vagrant/network_interface.rb', line 36

def gateway
  @gateway
end

#ip_addressObject (readonly)

Returns the value of attribute ip_address.



36
37
38
# File 'lib/nise-bosh-vagrant/network_interface.rb', line 36

def ip_address
  @ip_address
end

#nameObject (readonly)

Returns the value of attribute name.



36
37
38
# File 'lib/nise-bosh-vagrant/network_interface.rb', line 36

def name
  @name
end

#network_maskObject (readonly)

Returns the value of attribute network_mask.



36
37
38
# File 'lib/nise-bosh-vagrant/network_interface.rb', line 36

def network_mask
  @network_mask
end

#vbox_nameObject (readonly)

Returns the value of attribute vbox_name.



36
37
38
# File 'lib/nise-bosh-vagrant/network_interface.rb', line 36

def vbox_name
  @vbox_name
end

Class Method Details

.detectObject



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
# File 'lib/nise-bosh-vagrant/network_interface.rb', line 4

def self.detect
  device_list = `VBoxManage list bridgedifs`
  raise "VBoxManage command not found" unless $?.success?

  device_list.split(/^$\n/).inject([]) { |devices, device|
    device = Hash[*device.split("\n").map { |line| line.match(/^([^:]+): *(.*)/).captures }.flatten]
    kernel_device_name = device["Name"].split(":")[0]

    ifconfig = `ifconfig #{kernel_device_name}`
    if match = ifconfig.match(/inet ([.0-9]+) netmask 0x([0-9a-f]+) broadcast/) # BSD
      address = match[1]
      mask = match[2].unpack("a2a2a2a2").map { |block| block.hex }.join(".")
      gateway = `route -n get -ifscope #{kernel_device_name} 0.0.0.0 2>&1 | grep gateway | awk '{print $2}'`.strip
    elsif match = ifconfig.match(/inet addr:([.0-9]+)  Bcast:[.0-9]+  Mask:([.0-9]+)/) # Linux
      address = match[1]
      mask = match[2]
      gateway = `ip route show oif #{kernel_device_name} | grep 'default via' | awk '{print $3}'`.strip
    else
      next devices
    end

    devices << self.new(device["Name"], address, mask, gateway)
    devices
  }
end

.find(ip_address) ⇒ Object



30
31
32
33
34
# File 'lib/nise-bosh-vagrant/network_interface.rb', line 30

def self.find(ip_address)
  self.detect.find do |device|
    device.same_network?(ip_address)
  end
end

.ip_address_to_int(ip_address) ⇒ Object



54
55
56
# File 'lib/nise-bosh-vagrant/network_interface.rb', line 54

def self.ip_address_to_int(ip_address)
    ip_address.split(".").map{|s|s.to_i}.pack("C*").unpack("N")[0]
end

Instance Method Details

#same_network?(address) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
52
# File 'lib/nise-bosh-vagrant/network_interface.rb', line 49

def same_network?(address)
  (self.class.ip_address_to_int(@ip_address) & self.class.ip_address_to_int(@network_mask)) ==
  (self.class.ip_address_to_int(address) & self.class.ip_address_to_int(@network_mask))
end