Class: VagrantPlugins::GuestMSYS2::GuestNetwork
- Inherits:
-
Object
- Object
- VagrantPlugins::GuestMSYS2::GuestNetwork
- Defined in:
- lib/vagrant-guest-msys2/guest_network.rb
Overview
Manages the remote Windows guest network.
Instance Method Summary collapse
-
#configure_dhcp_interface(nic_index, net_connection_id) ⇒ Object
Configures the specified interface for DHCP.
-
#configure_static_interface(nic_index, net_connection_id, ip, netmask) ⇒ Object
Configures the specified interface using a static address.
-
#initialize(communicator) ⇒ GuestNetwork
constructor
A new instance of GuestNetwork.
-
#is_dhcp_enabled(nic_index) ⇒ Boolean
Checks to see if the specified NIC is currently configured for DHCP.
-
#network_adapters ⇒ Array
Returns an array of all NICs on the guest.
-
#set_all_networks_to_work ⇒ Object
Sets all networks on the guest to ‘Work Network’ mode.
Constructor Details
#initialize(communicator) ⇒ GuestNetwork
Returns a new instance of GuestNetwork.
8 9 10 11 |
# File 'lib/vagrant-guest-msys2/guest_network.rb', line 8 def initialize(communicator) @logger = Log4r::Logger.new("vagrant::guest::msys2::guestnetwork") @communicator = communicator end |
Instance Method Details
#configure_dhcp_interface(nic_index, net_connection_id) ⇒ Object
Configures the specified interface for DHCP
67 68 69 70 71 72 73 |
# File 'lib/vagrant-guest-msys2/guest_network.rb', line 67 def configure_dhcp_interface(nic_index, net_connection_id) @logger.info("Configuring NIC #{net_connection_id} for DHCP") if !is_dhcp_enabled(nic_index) netsh = "netsh interface ip set address \"#{net_connection_id}\" dhcp" @communicator.execute(netsh) end end |
#configure_static_interface(nic_index, net_connection_id, ip, netmask) ⇒ Object
Configures the specified interface using a static address
81 82 83 84 85 86 |
# File 'lib/vagrant-guest-msys2/guest_network.rb', line 81 def configure_static_interface(nic_index, net_connection_id, ip, netmask) @logger.info("Configuring NIC #{net_connection_id} using static ip #{ip}") #netsh interface ip set address "Local Area Connection 2" static 192.168.33.10 255.255.255.0 netsh = "netsh interface ip set address \"#{net_connection_id}\" static #{ip} #{netmask}" @communicator.execute(netsh) end |
#is_dhcp_enabled(nic_index) ⇒ Boolean
Checks to see if the specified NIC is currently configured for DHCP.
53 54 55 56 57 58 59 60 61 |
# File 'lib/vagrant-guest-msys2/guest_network.rb', line 53 def is_dhcp_enabled(nic_index) cmd = <<-EOH if (Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter "Index=#{nic_index} and DHCPEnabled=True") { exit 0 } exit 1 EOH @communicator.test(Util::CapHelpers.wrap_powershell(cmd)) end |
#network_adapters ⇒ Array
Returns an array of all NICs on the guest. Each array entry is a Hash of the NICs properties.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/vagrant-guest-msys2/guest_network.rb', line 17 def network_adapters @logger.debug("querying network adapters") # Note: without JSON escape because MACAddress and NetConnectionID # can't contain invalid characters cmd = <<-EOH.gsub(/^ {10}/, '') $adapters = Get-WmiObject -Class Win32_NetworkAdapter -Filter "MACAddress IS NOT NULL" [string[]] $json= $() foreach ($adapter in $adapters) { $json += "{` `"mac_address`": `"$($adapter.MACAddress)`",` `"net_connection_id`": `"$($adapter.NetConnectionID)`",` `"interface_index`": $([int]$adapter.InterfaceIndex),` `"index`": $([int]$adapter.Index)` }" } Write-Host "[$($json -Join ",")]" EOH output = "" @communicator.execute(Util::CapHelpers.wrap_powershell(cmd)) do |type, line| output = output + "#{line}" if type == :stdout && !line.nil? end adapters = [] JSON.parse(output).each do |nic| adapters << nic.inject({}){ |memo,(k,v)| memo[k.to_sym] = v; memo } end @logger.debug("#{adapters.inspect}") return adapters end |
#set_all_networks_to_work ⇒ Object
Sets all networks on the guest to ‘Work Network’ mode. This is to allow guest access from the host via a private IP on Win7 github.com/WinRb/vagrant-windows/issues/63
91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/vagrant-guest-msys2/guest_network.rb', line 91 def set_all_networks_to_work @logger.info("Setting all networks to 'Work Network'") cmd = <<-EOH # Get network connections $networkListManager = [Activator]::CreateInstance([Type]::GetTypeFromCLSID([Guid]"{DCB00C01-570F-4A9B-8D69-199FDBA5723B}")) $connections = $networkListManager.GetNetworkConnections() # Set network location to Private for all networks $connections | % {$_.GetNetwork().SetCategory(1)} EOH @communicator.execute(Util::CapHelpers.wrap_powershell(cmd)) end |