Skip to content

Instantly share code, notes, and snippets.

@nsnw
Created January 17, 2013 02: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 nsnw/4552872 to your computer and use it in GitHub Desktop.
Save nsnw/4552872 to your computer and use it in GitHub Desktop.
A hideous first-run at some Ruby - using Ohai - to print out IP addresses and MAC addresses for active interfaces.
require 'ohai'
o = Ohai::System.new
o.all_plugins
interfaces = o[:network][:interfaces]
info = Hash.new
interfaces.keys.each do |interface|
if interfaces[interface][:state] == "up" && interfaces[interface][:encapsulation] == "Ethernet"
info[interface] = Hash.new
addresses = interfaces[interface][:addresses]
addresses.keys.each do |address|
if addresses[address][:family] == "inet"
if info[interface][:ipv4].kind_of?(Array) == false
info[interface][:ipv4] = Array.new
end
info[interface][:ipv4].push(address)
end
if addresses[address][:family] == "inet6"
if info[interface][:ipv6].kind_of?(Array) == false
info[interface][:ipv6] = Array.new
end
info[interface][:ipv6].push(address)
end
if addresses[address][:family] == "lladdr"
info[interface][:mac_address] = address
end
end
end
end
info.keys.each do |ifname|
puts "[" + ifname + "]"
puts " MAC --> " + info[ifname][:mac_address]
if info[ifname].has_key?(:ipv4) == true
print " IPv4 --> "
info[ifname][:ipv4].each do |address|
print address + " "
end
end
print "\n"
if info[ifname].has_key?(:ipv6) == true
print " IPv6 --> "
info[ifname][:ipv6].each do |address|
print address + " "
end
end
print "\n\n"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment