如何在不利用其他 IP 地址的情况下从 Ruby 获取我机器的 IP 地址?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14112955/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 05:37:51  来源:igfitidea点击:

How to get my machine's IP address from Ruby without leveraging from other IP address?

rubysocketsip

提问by user1535147

I have searched everywhere but their solution requires some form of IP address. Here are the solutions i have found.

我到处搜索,但他们的解决方案需要某种形式的 IP 地址。以下是我找到的解决方案。

    require 'socket'
#METHOD 1
    ip = IPSocket.getaddress(Socket.gethostname)
    puts ip 

#METHOD 2
    host = Socket.gethostname
    puts host

#METHOD 3(uses Google's address)
    ip = UDPSocket.open {|s| s.connect("64.233.187.99", 1); s.addr.last}
    puts ip

#METHOD 4(uses gateway address)
    def local_ip
      orig, Socket.do_not_reverse_lookup = Socket.do_not_reverse_lookup, true  # turn off reverse DNS resolution temporarily

      UDPSocket.open do |s|
        s.connect '192.168.1.1', 1
        s.addr.last
      end
    ensure
      Socket.do_not_reverse_lookup = orig
    end

    ip=local_ip
    puts ip

All of them require IP address of someone. Is there a solution that does not use someone else's IP address? Preferably, platform independent.

所有这些都需要某人的 IP 地址。有没有不使用别人IP地址的解决方案?最好是平台独立的。

回答by Itay Grudev

Isn't the solution you are looking for just:

您正在寻找的解决方案不只是:

require 'socket'

addr_infos = Socket.ip_address_list

Since a machine can have multiple interfaces and multiple IP Addresses, this method returns an array of Addrinfo.

由于一台机器可以有多个接口和多个 IP 地址,因此此方法返回一个Addrinfo数组。

You can fetch the exact IP addresses like this:

您可以像这样获取确切的 IP 地址:

addr_infos.each do |addr_info|
  puts addr_info.ip_address
end

P.S. The question is a bit old, but shows up as the first item on Google and it doesn't contain the solution most people are looking for, so I decided to post it.

PS 这个问题有点老了,但在谷歌上显示为第一项,它不包含大多数人正在寻找的解决方案,所以我决定发布它。

Hope it helps.

希望能帮助到你。

回答by fuzzygroup

This is what I've been using in production for years:

这是我多年来在生产中一直使用的:

require 'socket'
ip = Socket.ip_address_list.detect{|intf| intf.ipv4_private?}
ip.ip_address

Works great; tested on aws and classical hosting

效果很好;在 aws 和经典托管上测试

回答by Eugene Rourke

require 'socket'
Socket::getaddrinfo(Socket.gethostname,"echo",Socket::AF_INET)[0][3]

quite like method 1, actually

很像方法1,实际上

回答by alk

As there is no such thing as a default ip-interface to a host (there does not need to be any ip-interface at all actually) all assumptions regarding nameing are vague, do not necessarily hold.

由于没有主机的默认 ip 接口(实际上根本不需要任何 ip 接口),所有关于命名的假设都是模糊的,不一定成立。

The value returned by gethostname()can be defined independently to any ip-setup, so it does not need to reflect a valid host in terms of a hostname which could be resolved to any ip-address.

返回的值gethostname()可以独立于任何 ip-setup 定义,因此它不需要根据可以解析为任何 ip-address 的主机名来反映有效主机。

From the POSIX system's API's view the only reliabe function to test for the availablily of (ip-)interfaces is the function getifaddrs(), which returns a list of all interfaces along with their parameters.

从 POSIX 系统的 API 的角度来看,唯一用于测试 (ip-) 接口可用性的可靠函数是 function getifaddrs(),它返回所有接口及其参数的列表。

As it looks as if Ruby's current Socket lib does not provide an interface to it, this (http://rubygems.org/gems/system-getifaddrs)gem based approach does seem to be the only way to go.

看起来 Ruby 的当前 Socket 库似乎没有为其提供接口,因此这种 (http://rubygems.org/gems/system-getifaddrs)基于 gem 的方法似乎是唯一的出路。

回答by rogerdpack

parse the output of the ipcommand?

解析ip命令的输出?

from https://gist.github.com/henriquemenezes/a99f13da957515023e78aea30d6c0a48

来自https://gist.github.com/henriquemenezes/a99f13da957515023e78aea30d6c0a48

gw = `ip route show`[/default.*/][/\d+\.\d+\.\d+\.\d+/]

or parse the output of the ipconfigcommand: https://stackoverflow.com/a/12632929/32453

或解析ipconfig命令的输出:https: //stackoverflow.com/a/12632929/32453