Class: Informo::Network

Inherits:
Object
  • Object
show all
Defined in:
lib/informo/network.rb

Overview

This class is used to provide information about the network configuration of the system and physical networking hardware available to the system.

Instance Method Summary collapse

Instance Method Details

#host_bus_adaptersObject

returns an array of installed HBAs



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/informo/network.rb', line 59

def host_bus_adapters
  list = Array.new
  
  `lspci`.each_line do |line|
    if line =~ /fib|hba/i        
      
      list.push($1) if line =~ /.+?:\s+(.+)/ 
    end
  end
  
  return list
end

#interface_details(dev) ⇒ Object

returns hash of the following info for a given network interface

  • ip address

  • subnet mask

  • broadcast address

  • mtu



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/informo/network.rb', line 29

def interface_details(dev)
  details = Hash.new
  details["ip6"] = Array.new
  count = -1 
  
  if File.exists?("/sbin/ip") 
    `ip addr show #{dev}`.each_line do |line|
      details["mtu"] = $1 if line =~ /mtu (\d+)/
  
      if line =~ /inet\s(\d+\.\d+\.\d+\.\d+)\/(\d+)\s+brd\s(\d+\.\d+\.\d+\.\d+)\sscope.+#{dev}$/
        details["ipaddr"],details["prefix"],details["broadcast"] = $1, $2, $3 
      end
      
      if dev != /:/
        if line =~ /inet6\s(.+?)\/(\d+)\sscope/
          count += 1
          details["ip6"][count] = Hash.new
          details["ip6"][count]["ipaddr"],details["ip6"][count]["prefix"] = $1, $2 
        end
      end
    end
  else
    `ifconfig #{dev}`.each_line do |line|
    end
  end
  
  return details
end

#interfacesObject

returns an array of available network interfaces



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/informo/network.rb', line 8

def interfaces
  interfaces = Array.new
  if File.exists?("/sbin/ip")
    `ip a`.each_line do |line|
      interfaces.push($1) if line =~ /scope.+?\s(\w+\d+(:.+?)?)$/  
    end
  else
    `ifconfig -a`.each_line do |line|
      interfaces.push($1) if line =~ /^(\w+\d+(:.+?)?)\s+Link/
    end
  end
  
  return interfaces
end