Ruby - Platform independent way to determine IPs of all network interfaces?

Is there an easy way in Ruby for me to get a list of the IP addresses for all network interfaces? It needs to work in Linux/Win/OSX and I'd prefer to not have to parse ifconfig/ipconfig unless I absolutely have to.

-------------Problems Reply------------

As of Ruby 2.1, Socket#getifaddrs is available:

001:0> require 'socket'
=> true
002:0> { |i| i.addr.ip_address if i.addr.ipv4? }.compact
=> ["", "", ""]

Check out the following post:

There's also a discussion on the solution anshul posted in the comments.

I don't think ruby has a standard api for this but under some assumptions this should be fairly reliable across platforms:

require 'socket'
Socket::getaddrinfo(Socket.gethostname, 'echo', Socket::AF_INET).map { |x| x[3] }

Here we are assuming quite a few things like the machine having a local hostname pointing to the correct ip addresses. So, this is definitely not completely reliable but it's platform independent and works on the common setups.

Edit: If you decide to get down to parsing ifconfig, consider forking ruby-ifconfig. It claims to do that on most non-windows platforms already.

Category:ruby Views:1 Time:2010-03-16

Related post

  • Is there a way to attach Ruby Net::HTTP request to a specific IP address / network interface? 2010-06-09

    Im looking a way to use different IP addresses for each GET request with standard Net::HTTP library. Server has 5 ip addresses and assuming that some API`s are blocking access when request limit per IP is reached. So, only way to do it - use another

  • Is Ruby a good option for writing platform independent desktop applications 2012-02-21

    Is Ruby a good option for writing (possibly and partially) platform independent desktop applications? Is there any supported libraries to write write code for window,linux,mac Operating systems I know Java have capabilities to write desktop applicati

  • Most appropriate platform independent development language 2009-05-28

    A project is looming whereby some code that I will be writing may be deployed on any hardware that potential clients happen to have. Its a business application that will be running 24/7 so I envisage that most of the host machines will be server type

  • How to use node-set function in a platform-independent way? 2008-09-18

    I'm writing some xlst file which I want to use under linux and Windows. In this file I use node-set function which declared in different namespaces for MSXML and xsltproc ("urn:schemas-microsoft-com:xslt" and "" respectively).

  • Is there any platform-independent way to access the clipboard? 2009-12-07

    I have a small side project I want to write and have yet to decide on a language. Ultimately, I want a way to access and/or modify the clipboard in a platform-independent way. I'd like to learn a new language to do this, preferably something that: I'

  • Platform independent languages 2010-04-09

    I was searching a bit for another platform independent language like Java. Are there other P.I. languages? Thanks --------------Solutions------------- Platform independent isn't a perfectly well defined term. It can mean different things to different

  • Platform-independent C++ Debugger IDEs that follow pointers like Visual Studio 2010-06-29

    Visual Studio debugger automatically recognizes the types of pointers and shows the value of the variable or object pointed to by the pointer. Example screenshot for Ruby debugger here: This applies to debugger tooltips, watch windows, etc. I never h

  • Specifying the logging file location in Websphere in a platform-independent way 2010-09-03

    When using a Log4J RollingFileAppender on Websphere 7.0, how can I specify the location of the logging directory in the file, i.e. pick up Websphere's LOG_ROOT variable? --------------Solutions------------- Of course, it would be tri

  • Platform-independent way of detecting if git is installed 2011-01-04

    This is how I detect git in ruby: `which git 2>/dev/null` and $?.success? However, this is not cross-platform. It fails on non-unix systems or those without the which command (although I'm not sure what those are). I need a way to detect git that

  • JavaScript Platform Independent Line separator 2011-07-26

    Does something similar to Java's System.getProperty("line.separator"); exist in JavaScript? Edit: I am using the non-browser JavaScript environment Node.js --------------Solutions------------- I had the same problem and came across this quite old que

  • Platform independent storage of signed integers 2011-10-26

    I want to write signed integer values into a file in a platform independent way. If they were unsigned, I would just convert them from host byte order to LE (or BE) with the endian(3) family of functions. I'm not sure how to deal with signed integers

  • Java: How do I get a platform-independent new line character? 2008-10-16

    How do I get a platform-independent newline in Java? I can’t use "\n" everywhere. --------------Solutions------------- In addition to the line.separator property, if you are using java 1.5 or later and the String.format (or other formatting methods)

  • I need platform-independent version of CharToOem. Does Boost have any? 2009-02-23

    I need to convert string to support multi-language messaging in my client-server app. Can I find it in boost? --------------Solutions------------- For platform-independent codepage conversions you can use libiconv library. Anyway, why bother using co

  • Is WCF platform independent? 2009-05-13

    I just need to know if WCF is platform independent like Webservices? Can the functions in WCF be accessed by Java and Php? Thanks --------------Solutions------------- Yes sure - WCF itself will run on Windows only - but the services it provides can b

  • Platform Independent, Lightweight Programming Language 2009-06-11

    I need to write an extremely lightweight program (trying to get below 8Kb) that performs some simple math. The language also needs to be platform independent. Which language do you think would work the best? (Oh, and no frameworks allowed.) ---------

  • Is java Runtime.exec(String[]) platform independent? 2009-07-03

    I had some code that ran commands through Runtime.getRuntime.exec(String), and it worked on Windows. When I moved the code to Linux, it broke, and the only way of fixing it was to switch to the exec(String[]) version. If I leave things this way, will

  • Python Math Library Independent of C Math Library and Platform Independent? 2009-07-21

    Does the built-in Python math library basically use C's math library or does Python have a C-independent math library? Also, is the Python math library platform independent? --------------Solutions------------- at the bottom of the page it says: Note

  • printf with sizeof on 32 vs 64 platforms: how do I handle format code in platform independant manner? 2009-09-10

    Very easy question for you Gurus but I haven't found the answer yet with about a good amount of searching. I have some code that prints the amount of memory used by the program. The line is similar to this: printf("The about of RAM used is %u", anInt

  • Platform-independent permalinks - best practices? 2009-09-14

    I'm trying to figure out a generic permalinks structure for blogging, in order to be platform-independent. I know that Wordpress supports permalinks, and has some plugins for various permalinks style migration, but I also have to get it working in Fl

Copyright (C), All Rights Reserved.

processed in 0.091 (s). 11 q(s)