Class: HostInfo

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

Class Method Summary collapse

Class Method Details

.broadcast(interface = nil) ⇒ Object

Returns string containing the broadcast address for a particular network interface. If interface is not passed as an argument, method assumes primary interface.



128
129
130
131
132
133
134
135
136
137
138
# File 'lib/hostinfo.rb', line 128

def self.broadcast(interface=nil)
  interface = self.default_network_interface() unless interface != nil
  if interface.nil? then return nil end
  
  begin
    ipaddr = self.ipv4(interface)
    return @hst[:network][:interfaces][interface][:addresses][ipaddr][:broadcast]
  rescue
    return nil
  end
end

.default_network_interfaceObject

Returns string containing default network interface



52
53
54
55
56
57
58
# File 'lib/hostinfo.rb', line 52

def self.default_network_interface()
  begin
    return interface = @hst[:network][:default_interface]
  rescue
    return nil
  end
end

.domainObject

Returns string containing the domain name of the host



47
48
49
# File 'lib/hostinfo.rb', line 47

def self.domain()
  return @hst[:domain]
end

.hostnameObject

Returns string of fully qualified domain name for host



42
43
44
# File 'lib/hostinfo.rb', line 42

def self.hostname()
  return @hst[:fqdn]
end

.ipv4(interface = nil) ⇒ Object

Returns string containing IPv4 address. If interface is not passed as an argument, method assumes primary interface.



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
# File 'lib/hostinfo.rb', line 61

def self.ipv4(interface=nil)
  interface = self.default_network_interface() unless interface != nil
  if interface.nil? then return nil end
    
    begin
      ipv4addr_array = Array.new
      output_array = Array.new
      @hst[:network][:interfaces][interface][:addresses].each_key do |key|
        if key[Resolv::IPv4::Regex] != nil
          ipv4addr_array << key 
        end
      end

      if ipv4addr_array.nil? then return nil end

      ipv4addr_array.each do |ipv4ip|
        if (@hst[:network][:interfaces][interface][:addresses][ipv4ip][:family] == "inet")
          output_array << ipv4ip
        end
      end
      return output_array
    rescue
      return nil
    end
end

.ipv6(interface = nil) ⇒ Object

Returns string containing IPv6 address. If interface is not passed as an argument, method assumes primary interface.



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/hostinfo.rb', line 88

def self.ipv6(interface=nil)
  interface = self.default_network_interface() unless interface != nil
  if interface.nil? then return nil end

  begin
    ipv6addr_array = Array.new
    output_array = Array.new
    @hst[:network][:interfaces][interface][:addresses].each_key do |key|
      if key[Resolv::IPv6::Regex] != nil
        ipv6addr_array << key 
      end
    end
    
    if ipv6addr_array.nil? then return nil end
      
    ipv6addr_array.each do |ipv6ip|
      if (@hst[:network][:interfaces][interface][:addresses][ipv6ip][:family] == "inet6")
        output_array << ipv6ip
      end
    end
    return output_array
  rescue
    return nil
  end
end

.mac_address(interface = nil) ⇒ Object

Returns string containing the MAC address for a particular network interface. If interface is not passed as an argument, method assumes primary interface.



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/hostinfo.rb', line 141

def self.mac_address(interface=nil)
  interface = self.default_network_interface() unless interface != nil
  if interface.nil? then return nil end
  
  begin
    ipaddr = self.ipv4(interface)
    macaddr =  @hst[:network][:interfaces][interface][:addresses].to_s[/[0-9a-f]{2}\:[0-9a-f]{2}\:[0-9a-f]{2}\:[0-9a-f]{2}\:[0-9a-f]{2}\:[0-9a-f]{2}/]
    if (@hst[:network][:interfaces][interface][:addresses][macaddr][:family] == "lladdr")
      return macaddr
    else
      return nil
    end
  rescue
    return nil
  end
end

.netmask(interface = nil) ⇒ Object

Returns string containing the netmask for a particular network interface. If interface is not passed as an argument, method assumes primary interface.



115
116
117
118
119
120
121
122
123
124
125
# File 'lib/hostinfo.rb', line 115

def self.netmask(interface=nil)
  interface = self.default_network_interface() unless interface != nil
  if interface.nil? then return nil end
  
  begin
    ipaddr = self.ipv4(interface)
    return @hst[:network][:interfaces][interface][:addresses][ipaddr][:netmask]
  rescue
    return nil
  end
end

.osObject

Returns string of shortened OS type



11
12
13
# File 'lib/hostinfo.rb', line 11

def self.os()
  return @hst_os
end

.os_longObject

Returns string of verbose OS type



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/hostinfo.rb', line 16

def self.os_long()
  os_long = case @hst_os
    when "fedora" then self.first_line("/etc/fedora-release")
    when "redhat" then self.first_line("/etc/redhat-release")
    when "centos" then self.first_line("/etc/redhat-release")
    when "debian" then self.debissue()
    when "ubuntu" then self.ubuissue()
    when "mac_os_x" then "Mac OS X Version " + self.version()
  end
  return os_long
end

.ssh_pub_key(type = nil) ⇒ Object

Returns string of current users’ public SSH key. If type is not passed as an argument, method assumes DSA.



159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/hostinfo.rb', line 159

def self.ssh_pub_key(type=nil)
  if type.nil?
    return @hst[:keys][:ssh][:host_dsa_public]
  else
    begin
      key_type = "host_" + type.downcase + "_public"
      return @hst[:keys][:ssh][key_type]
    rescue
      return nil
    end
  end    
end

.versionObject

Returns string of the numerical version of the OS (unless Debian or Ubuntu)



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/hostinfo.rb', line 29

def self.version()
  ver = case @hst_os
    when "fedora" then self.os_long().split(" ")[2]
    when "centos" then self.os_long().split(" ")[2]
    when "redhat" then self.os_long().split(" ")[2]
    when "debian" then self.os_long().split(" ")[2]
    when "ubuntu" then self.os_long().split(" ")[1]
    when "mac_os_x" then @hst[:platform_version]
  end
  return ver 
end