A little method I cobbled together to parse the output of ipconfig utility on MS Windows:
# read_ipconfig()
# @return [Hash] Nested hash of the Windows IP Configuration
#
# Usage:
# ipconfig = read_ipconfig()
#
def read_ipconfig
#
filepath = File.join(ENV["TEMP"],"ipconfig.txt")
cmd = %[ipconfig /allcompartments /all > "#{filepath}"]
system(cmd)
Kernel.sleep(3)
ipconfig = {}
key, subkey = nil
#
File.foreach(filepath) do |line|
next if line.strip.empty?
if line =~ /^(Windows IP Configuration)/
ipconfig["name"]= 'Windows IP Configuration'
next
end
if line =~ /^\w/
key = line.chomp.chomp.strip
ipconfig[key]= {}
next
end
if line =~ /^(\s+\w)/
ary = line.chomp.chomp.strip.split(':')
subkey = ary[0].split('.').first.strip
val = ary[1].strip rescue ""
ipconfig[key][subkey]= val
next
end
end
#
File.delete(filepath)
#
return ipconfig
#
end
![]()