Skip to content

Instantly share code, notes, and snippets.

@mrunalp
Created March 18, 2014 21:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrunalp/9629571 to your computer and use it in GitHub Desktop.
Save mrunalp/9629571 to your computer and use it in GitHub Desktop.
Get IP address for an interface in ruby.
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