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

后端 未结 5 486
说谎
说谎 2020-12-14 07:43

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

    require \'socket\'
#METHOD 1
    ip          


        
5条回答
  •  失恋的感觉
    2020-12-14 08:05

    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.

    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.

    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.

    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.

提交回复
热议问题