Created
March 18, 2014 21:03
-
-
Save mrunalp/9629571 to your computer and use it in GitHub Desktop.
Get IP address for an interface in ruby.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'socket' | |
require 'ipaddr' | |
# From ioctls.h | |
SIOCGIFADDR = 0x8915 | |
def ip_address(iface) | |
sock = UDPSocket.new | |
buf = [iface,""].pack('a16h16') | |
sock.ioctl(SIOCGIFADDR, buf); | |
sock.close | |
buf[20..24].unpack("CCCC").join(".") | |
end | |
puts ip_address('em1') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment