Skip to content

Instantly share code, notes, and snippets.

@mitchellh
Last active December 16, 2015 12:39
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 mitchellh/5436471 to your computer and use it in GitHub Desktop.
Save mitchellh/5436471 to your computer and use it in GitHub Desktop.
Reading forwarded ports in VMware Workstation on Windows
results = {}
results[:tcp] = {}
results[:udp] = {}
Win32::Registry::HKEY_LOCAL_MACHINE.open('SOFTWARE\VMware, Inc.\VMnetLib\VMnetConfig') do |reg|
# Get each of the vmnets
reg.each_key do |vmnet|
reg.open(vmnet) do |vmnet_reg|
# If this network doesn't include the NAT key, we
# don't really care about it.
is_nat = true
is_nat = false if !vmnet_reg.keys.include?("NAT")
if is_nat
vmnet_reg.open("NAT") do |vmnet_nat|
begin
is_nat = vmnet_nat["UseNAT"] == 1
rescue Win32::Registry::Error
is_nat = false
end
end
end
next if !is_nat
[:tcp, :udp].each do |protocol|
begin
vmnet_reg.open("NAT\\#{protocol.to_s.upcase}Forward") do |vmnet_forward|
vmnet_forward.each_value do |name, type, value|
# Ignore any values that aren't ports
next if name !~ /^\d+$/
# Attempt to get the description
description = ""
begin
description = vmnet_forward.read_bin("#{name}Description")
rescue Win32::Registry::Error
# Ignore
end
results[protocol][name.to_i] = {
:description => description,
:virtual_address => value,
:vmnet => vmnet
}
end
end
rescue Win32::Registry::Error
# Ignore, no forwarded ports of this type
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment