Class: LinuxIPParsePlatform

Inherits:
IPParsePlatform show all
Defined in:
lib/ipparse/platforms/linux.rb

Overview

VMware Continuent Tungsten Replicator Copyright © 2015 VMware, Inc. All rights reserved.

Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Initial developer(s): Jeff Mace

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from IPParsePlatform

#add_ipv4, #add_ipv6, inherited, #initialize, subclasses

Constructor Details

This class inherits a constructor from IPParsePlatform

Class Method Details

.supports_platform?(platform) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
22
23
24
25
# File 'lib/ipparse/platforms/linux.rb', line 19

def self.supports_platform?(platform)
  if platform.downcase() =~ /linux/
    true
  else
    false
  end
end

Instance Method Details

#get_raw_ip_configurationObject



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/ipparse/platforms/linux.rb', line 27

def get_raw_ip_configuration
  path = `which ifconfig 2>/dev/null`.chomp()
  if path == ""
    path = "/sbin/ifconfig"
  end
  
  results = `export LANG=en_US; #{path} -a`
  if results == false
    raise "Unable to collect IP configuration from ifconfig"
  else
    return results
  end
end

#parse(raw) ⇒ Object



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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/ipparse/platforms/linux.rb', line 41

def parse(raw)
  name_regex = Regexp.compile(/^([a-zA-Z0-9]+)/)
  encapsulation_regex = Regexp.compile(/Link encap:(\S+)/)
  flags_regex = Regexp.compile(/flags=([0-9]+)\<([A-Z,]+)\>/)
  inet4_regex1 = Regexp.compile(/inet addr:([0-9\.]+)[ ]+(Bcast:[0-9\.]+ )?[ ]+Mask:([0-9\.]+)/)
  inet4_regex2 = Regexp.compile(/inet ([0-9\.]+)[ ]+netmask ([0-9\.]+)[ ]+(broadcast [0-9\.]+ )?/)
  inet6_regex1 = Regexp.compile(/inet6 addr:[ ]*([a-f0-9:]+)\/([0-9]+)/)
  inet6_regex2 = Regexp.compile(/inet6 ([a-f0-9:]+)[ ]*prefixlen ([0-9]+)/)

  raw.split("\n\n").each{
    |ifconfig|
    include_interface = false
    
    begin
      encapsulation = encapsulation_regex.match(ifconfig)[1]
      
      if encapsulation.downcase() == "ethernet"
        include_interface = true
      end
    rescue
      # Catch the exception and move on
    end
    
    begin
      flags = flags_regex.match(ifconfig)[2]
      flags = flags.split(",")

      if flags.include?("LOOPBACK") == false
        include_interface = true
      end
    rescue
      # Catch the exception and move on
    end
    
    if include_interface == false
      next
    end
    
    name = name_regex.match(ifconfig)[1]
    if name == nil
      raise "Unable to parse IP configuration because a valid name does not exist"
    end
    
    m1 = inet4_regex1.match(ifconfig)
    m2 = inet4_regex2.match(ifconfig)
    if m1
      add_ipv4(name, m1[1], m1[3])
    elsif m2
      add_ipv4(name, m2[1], m2[2])
    end
    
    m1 = inet6_regex1.match(ifconfig)
    m2 = inet6_regex2.match(ifconfig)
    if m1
      add_ipv6(name, m1[1], m1[2])
    elsif m2
      add_ipv6(name, m2[1], m2[2])
    end
  }
  
  return @interfaces
end