zoukankan      html  css  js  c++  java
  • ruby 取得ip

    A server typically has more than one interface, at least one private and one public.
    
    Since all the answers here deal with this simple scenario, a cleaner way is to ask Socket for the current ip_address_list() as in:
    
    
    
    require 'socket'
    
    def my_first_private_ipv4
      Socket.ip_address_list.detect{|intf| intf.ipv4_private?}
    end
    
    def my_first_public_ipv4
      Socket.ip_address_list.detect{|intf| intf.ipv4? and !intf.ipv4_loopback? and !intf.ipv4_multicast? and !intf.ipv4_private?}
    end
    
    Both returns an Addrinfo object, so if you need a string you can use the ip_address() method, as in:
    
    ip= my_first_public_ipv4.ip_address unless my_first_public_ipv4.nil?
  • 相关阅读:
    [solr]
    [solr]
    [Linux] CentOS 加入开机启动
    [Linux] VirtualBox
    [Eclipse]
    [JBoss]
    [solr]
    [solr]
    [solr]
    [solr]
  • 原文地址:https://www.cnblogs.com/laoquans/p/3338235.html
Copyright © 2011-2022 走看看