github.com/vmware/govmomi@v0.51.0/cli/vm/ip.go (about)

     1  // © Broadcom. All Rights Reserved.
     2  // The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.
     3  // SPDX-License-Identifier: Apache-2.0
     4  
     5  package vm
     6  
     7  import (
     8  	"context"
     9  	"flag"
    10  	"fmt"
    11  	"strings"
    12  	"time"
    13  
    14  	"github.com/vmware/govmomi/cli"
    15  	"github.com/vmware/govmomi/cli/esx"
    16  	"github.com/vmware/govmomi/cli/flags"
    17  	"github.com/vmware/govmomi/object"
    18  )
    19  
    20  type ip struct {
    21  	*flags.OutputFlag
    22  	*flags.SearchFlag
    23  
    24  	esx  bool
    25  	all  bool
    26  	v4   bool
    27  	wait time.Duration
    28  	nic  string
    29  }
    30  
    31  func init() {
    32  	cli.Register("vm.ip", &ip{})
    33  }
    34  
    35  func (cmd *ip) Register(ctx context.Context, f *flag.FlagSet) {
    36  	cmd.OutputFlag, ctx = flags.NewOutputFlag(ctx)
    37  	cmd.OutputFlag.Register(ctx, f)
    38  
    39  	cmd.SearchFlag, ctx = flags.NewSearchFlag(ctx, flags.SearchVirtualMachines)
    40  	cmd.SearchFlag.Register(ctx, f)
    41  
    42  	f.BoolVar(&cmd.esx, "esxcli", false, "Use esxcli instead of guest tools")
    43  	f.BoolVar(&cmd.all, "a", false, "Wait for an IP address on all NICs")
    44  	f.StringVar(&cmd.nic, "n", "", "Wait for IP address on NIC, specified by device name or MAC")
    45  	f.BoolVar(&cmd.v4, "v4", false, "Only report IPv4 addresses")
    46  	f.DurationVar(&cmd.wait, "wait", time.Hour, "Wait time for the VM obtain an IP address")
    47  }
    48  
    49  func (cmd *ip) Usage() string {
    50  	return "VM..."
    51  }
    52  
    53  func (cmd *ip) Description() string {
    54  	return `List IPs for VM.
    55  
    56  By default the vm.ip command depends on vmware-tools to report the 'guest.ipAddress' field and will
    57  wait until it has done so.  This value can also be obtained using:
    58  
    59    govc vm.info -json $vm | jq -r .virtualMachines[].guest.ipAddress
    60  
    61  When given the '-a' flag, only IP addresses for which there is a corresponding virtual nic are listed.
    62  If there are multiple nics, the listed addresses will be comma delimited.  The '-a' flag depends on
    63  vmware-tools to report the 'guest.net' field and will wait until it has done so for all nics.
    64  Note that this list includes IPv6 addresses if any, use '-v4' to filter them out.  IP addresses reported
    65  by tools for which there is no virtual nic are not included, for example that of the 'docker0' interface.
    66  
    67  These values can also be obtained using:
    68  
    69    govc vm.info -json $vm | jq -r .virtualMachines[].guest.net[].ipConfig.ipAddress[].ipAddress
    70  
    71  When given the '-n' flag, filters '-a' behavior to the nic specified by MAC address or device name.
    72  
    73  The 'esxcli' flag does not require vmware-tools to be installed, but does require the ESX host to
    74  have the /Net/GuestIPHack setting enabled.
    75  
    76  The 'wait' flag default to 1hr (original default was infinite).  If a VM does not obtain an IP within
    77  the wait time, the command will still exit with status 0.
    78  
    79  Examples:
    80    govc vm.ip $vm
    81    govc vm.ip -wait 5m $vm
    82    govc vm.ip -a -v4 $vm
    83    govc vm.ip -n 00:0c:29:57:7b:c3 $vm
    84    govc vm.ip -n ethernet-0 $vm
    85    govc host.esxcli system settings advanced set -o /Net/GuestIPHack -i 1
    86    govc vm.ip -esxcli $vm`
    87  }
    88  
    89  func (cmd *ip) Process(ctx context.Context) error {
    90  	if err := cmd.OutputFlag.Process(ctx); err != nil {
    91  		return err
    92  	}
    93  	if err := cmd.SearchFlag.Process(ctx); err != nil {
    94  		return err
    95  	}
    96  	return nil
    97  }
    98  
    99  func (cmd *ip) Run(ctx context.Context, f *flag.FlagSet) error {
   100  	c, err := cmd.Client()
   101  	if err != nil {
   102  		return err
   103  	}
   104  
   105  	vms, err := cmd.VirtualMachines(f.Args())
   106  	if err != nil {
   107  		return err
   108  	}
   109  
   110  	var get func(*object.VirtualMachine, context.Context) (string, error)
   111  
   112  	if cmd.esx {
   113  		get = func(vm *object.VirtualMachine, deadline context.Context) (string, error) {
   114  			guest := esx.NewGuestInfo(c)
   115  
   116  			ticker := time.NewTicker(time.Millisecond * 500)
   117  			defer ticker.Stop()
   118  
   119  			for {
   120  				select {
   121  				case <-ticker.C:
   122  					ip, err := guest.IpAddress(ctx, vm)
   123  					if err != nil {
   124  						return "", err
   125  					}
   126  
   127  					if ip != "0.0.0.0" {
   128  						return ip, nil
   129  					}
   130  				case <-deadline.Done():
   131  					return "", nil
   132  				}
   133  			}
   134  		}
   135  	} else {
   136  		var hwaddr []string
   137  		if cmd.nic != "" {
   138  			hwaddr = strings.Split(cmd.nic, ",")
   139  		}
   140  
   141  		get = func(vm *object.VirtualMachine, deadline context.Context) (string, error) {
   142  			if cmd.all || hwaddr != nil {
   143  				macs, err := vm.WaitForNetIP(deadline, cmd.v4, hwaddr...)
   144  				if err != nil {
   145  					return "", err
   146  				}
   147  
   148  				var ips []string
   149  				for _, addrs := range macs {
   150  					ips = append(ips, addrs...)
   151  				}
   152  				return strings.Join(ips, ","), nil
   153  			}
   154  			return vm.WaitForIP(deadline, cmd.v4)
   155  		}
   156  	}
   157  
   158  	for _, vm := range vms {
   159  		deadline, cancel := context.WithDeadline(ctx, time.Now().Add(cmd.wait))
   160  
   161  		ip, err := get(vm, deadline)
   162  		if err != nil {
   163  			if deadline.Err() != context.DeadlineExceeded {
   164  				cancel()
   165  				return err
   166  			}
   167  		}
   168  
   169  		cancel()
   170  
   171  		if ip == "" {
   172  			continue
   173  		}
   174  
   175  		// TODO(PN): Display inventory path to VM
   176  		fmt.Fprintf(cmd, "%s\n", ip)
   177  	}
   178  
   179  	return nil
   180  }