A hideous first-run at some Ruby - using Ohai - to print out IP addresses and MAC addresses for active interfaces.
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 '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