Ruby - 与平台无关的方法来确定所有网络接口的IP? [英] Ruby - Platform independent way to determine IPs of all network interfaces?
本文介绍了Ruby - 与平台无关的方法来确定所有网络接口的IP?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Ruby中有一种简单的方法可以获取所有网络接口的IP地址列表吗?它需要在Linux / Win / OSX中工作,我宁愿不必解析ifconfig / ipconfig,除非我绝对必须。
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.
推荐答案
从Ruby 2.1开始,Socket#getifaddrs可用:
As of Ruby 2.1, Socket#getifaddrs is available:
001:0> require 'socket'
=> true
002:0> Socket.getifaddrs.map { |i| i.addr.ip_address if i.addr.ipv4? }.compact
=> ["127.0.0.1", "192.168.1.121", "192.168.1.181"]
这篇关于Ruby - 与平台无关的方法来确定所有网络接口的IP?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文