module VirtualboxWSL2::WSL2Provider

Public Instance Methods

ssh_info() click to toggle source

Returns the SSH info for accessing the VirtualBox VM.

# File lib/virtualbox_WSL2.rb, line 59
def ssh_info
  # If the VM is not running that we can't possibly SSH into it
  return nil if state.id != :running

  # Return what we know. The host is always "127.0.0.1" because
  # VirtualBox VMs are always local. The port we try to discover
  # by reading the forwarded ports.
  host_ip = "127.0.0.1"

  # If we run on WSL2, we need to ssh through Windows IP, which is set as the default route
  if Vagrant::Util::Platform.wsl?
    result = Vagrant::Util::Subprocess.execute("/bin/sh", "-c", "ip route | grep default | grep -Po '\\d+.\\d+.\\d+.\\d+'")
    if result.exit_code == 0
      host_ip = result.stdout.strip
    end
  end

  return {
    host: host_ip,
    port: @driver.ssh_port(@machine.config.ssh.guest_port)
  }
end