# \[code\] read\_ipconfig()

**URL:** <https://forums.sketchup.com/t/code-read-ipconfig/116689>\
**Category:** Ruby API\
**Created:** [February 17, 2020, 8:46pm UTC](https://forums.sketchup.com/t/code-read-ipconfig/116689 "2020-02-17T20:46:56Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![DanRathbun](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/danrathbun/32/500_2.png) [@DanRathbun](https://forums.sketchup.com/u/DanRathbun)\
**Post date:** [February 17, 2020, 8:46pm UTC](https://forums.sketchup.com/t/code-read-ipconfig/116689/1 "2020-02-17T20:46:56Z")

</div>

A little method I cobbled together to parse the output of `ipconfig` utility on MS Windows:

```ruby
# 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

```

💡
