Class: TestLab::Interface

Inherits:
ZTK::DSL::Base
  • Object
show all
Defined in:
lib/testlab/interface.rb

Overview

Interface Class

Author:

  • Zachary Patten <zachary AT jovelabs DOT com>

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Interface

Returns a new instance of Interface.



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/testlab/interface.rb', line 21

def initialize(*args)
  @ui = TestLab.ui

  @ui.logger.debug { "Loading Interface" }
  super(*args)

  self.address ||= generate_ip
  self.mac     ||= generate_mac

  @ui.logger.debug { "Interface '#{self.id}' Loaded" }
end

Instance Method Details

#cidrObject

CIDR mask for the interface



39
40
41
# File 'lib/testlab/interface.rb', line 39

def cidr
  TestLab::Utility.cidr(self.address)
end

#generate_ipString

Generate IP address

Generates an RFC compliant private IP address.

Returns:

  • (String)

    A random, private IP address in the 192.168.0.1/24 range.



59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/testlab/interface.rb', line 59

def generate_ip
  crc32  = Zlib.crc32(self.id.to_s)
  offset = crc32.modulo(255)

  octets = [ 192..192,
             168..168,
             0..254,
             1..254 ]
  ip = Array.new
  for x in 1..4 do
    ip << octets[x-1].to_a[offset.modulo(octets[x-1].count)].to_s
  end
  "#{ip.join(".")}/24"
end

#generate_macString

Generate MAC address

Generates an RFC compliant private MAC address.

Returns:

  • (String)

    A random, private MAC address.



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/testlab/interface.rb', line 79

def generate_mac
  crc32  = Zlib.crc32(self.id.to_s)
  offset = crc32.modulo(255)

  digits = [ %w(0),
             %w(0),
             %w(0),
             %w(0),
             %w(5),
             %w(e),
             %w(0 1 2 3 4 5 6 7 8 9 a b c d e f),
             %w(0 1 2 3 4 5 6 7 8 9 a b c d e f),
             %w(5 6 7 8 9 a b c d e f),
             %w(3 4 5 6 7 8 9 a b c d e f),
             %w(0 1 2 3 4 5 6 7 8 9 a b c d e f),
             %w(0 1 2 3 4 5 6 7 8 9 a b c d e f) ]
  mac = ""
  for x in 1..12 do
    mac += digits[x-1][offset.modulo(digits[x-1].count)]
    mac += ":" if (x.modulo(2) == 0) && (x != 12)
  end
  mac
end

#ipObject

IP address for the interface



34
35
36
# File 'lib/testlab/interface.rb', line 34

def ip
  TestLab::Utility.ip(self.address)
end

#netmaskObject

Netmask for the interface



44
45
46
# File 'lib/testlab/interface.rb', line 44

def netmask
  TestLab::Utility.netmask(self.address)
end

#ptrObject

PTR record for the interface



49
50
51
# File 'lib/testlab/interface.rb', line 49

def ptr
  TestLab::Utility.ptr(self.address)
end