Class: Socket

Inherits:
Object show all
Defined in:
lib/sane/socket_ips.rb

Class Method Summary collapse

Class Method Details

.get_ips(hostname) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/sane/socket_ips.rb', line 13

def get_ips hostname
  begin

    socket_info = Socket.getaddrinfo(hostname, nil,
      Socket::AF_UNSPEC, Socket::SOCK_STREAM, nil,
      Socket::AI_CANONNAME).select{|type| type[0] == 'AF_INET'}

    raise if socket_info.length == 0
    ips = socket_info.map{|info| info[3]}
    return ips
  rescue => e # getaddrinfo failed...

    begin
       ipInt = gethostbyname(hostname)[3] # do a DNS lookup

       return ["%d.%d.%d.%d" % [ipInt[0].ord, ipInt[1].ord, ipInt[2].ord, ipInt[3].ord]]
    rescue SocketError # happens in certain instances...

       return nil
    end
  end
end

.get_local_ipsObject



9
10
11
# File 'lib/sane/socket_ips.rb', line 9

def get_local_ips
  get_ips(Socket.gethostname)
end