VMWARE Fusion get IP from multiple leases files

Change to try get IP from multiple dhcp leases files

Signed-off-by: Daehyeok Mun <daehyeok@gmail.com>
This commit is contained in:
Daehyeok Mun 2016-01-09 01:26:23 -07:00
parent 36abac3891
commit 278cc175a2
1 changed files with 28 additions and 13 deletions

View File

@ -449,19 +449,10 @@ func (d *Driver) vmdkPath() string {
} }
func (d *Driver) getIPfromDHCPLease() (string, error) { func (d *Driver) getIPfromDHCPLease() (string, error) {
var vmxfh *os.File
var dhcpfh *os.File
var vmxcontent []byte
var dhcpcontent []byte
var macaddr string
var err error
var lastipmatch string
var currentip string
var lastleaseendtime time.Time
var currentleadeendtime time.Time
// DHCP lease table for NAT vmnet interface var vmxfh *os.File
var dhcpfile = "/var/db/vmware/vmnet-dhcpd-vmnet8.leases" var vmxcontent []byte
var err error
if vmxfh, err = os.Open(d.vmxPath()); err != nil { if vmxfh, err = os.Open(d.vmxPath()); err != nil {
return "", err return "", err
@ -473,6 +464,7 @@ func (d *Driver) getIPfromDHCPLease() (string, error) {
} }
// Look for generatedAddress as we're passing a VMX with addressType = "generated". // Look for generatedAddress as we're passing a VMX with addressType = "generated".
var macaddr string
vmxparse := regexp.MustCompile(`^ethernet0.generatedAddress\s*=\s*"(.*?)"\s*$`) vmxparse := regexp.MustCompile(`^ethernet0.generatedAddress\s*=\s*"(.*?)"\s*$`)
for _, line := range strings.Split(string(vmxcontent), "\n") { for _, line := range strings.Split(string(vmxcontent), "\n") {
if matches := vmxparse.FindStringSubmatch(line); matches == nil { if matches := vmxparse.FindStringSubmatch(line); matches == nil {
@ -487,6 +479,29 @@ func (d *Driver) getIPfromDHCPLease() (string, error) {
} }
log.Debugf("MAC address in VMX: %s", macaddr) log.Debugf("MAC address in VMX: %s", macaddr)
// DHCP lease table for NAT vmnet interface
leasesFiles, _ := filepath.Glob("/var/db/vmware/*.leases")
for _, dhcpfile := range leasesFiles {
log.Debugf("Trying to find IP address in leases file: %s", dhcpfile)
if ipaddr, err := d.getIPfromDHCPLeaseFile(dhcpfile, macaddr); err == nil {
return ipaddr, err
}
}
return "", fmt.Errorf("IP not found for MAC %s in DHCP leases", macaddr)
}
func (d *Driver) getIPfromDHCPLeaseFile(dhcpfile, macaddr string) (string, error) {
var dhcpfh *os.File
var dhcpcontent []byte
var lastipmatch string
var currentip string
var lastleaseendtime time.Time
var currentleadeendtime time.Time
var err error
if dhcpfh, err = os.Open(dhcpfile); err != nil { if dhcpfh, err = os.Open(dhcpfile); err != nil {
return "", err return "", err
} }
@ -526,8 +541,8 @@ func (d *Driver) getIPfromDHCPLease() (string, error) {
} }
log.Debugf("IP found in DHCP lease table: %s", currentip) log.Debugf("IP found in DHCP lease table: %s", currentip)
return currentip, nil
return currentip, nil
} }
func (d *Driver) publicSSHKeyPath() string { func (d *Driver) publicSSHKeyPath() string {