2013-07-31 17:46:12 -04:00
|
|
|
package vmware
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// VMnetNatConfIPFinder finds the IP address of the host machine by
|
|
|
|
// retrieving the IP from the vmnetnat.conf. This isn't a full proof
|
|
|
|
// technique but so far it has not failed.
|
|
|
|
type VMnetNatConfIPFinder struct{}
|
|
|
|
|
|
|
|
func (*VMnetNatConfIPFinder) HostIP() (string, error) {
|
2013-08-02 02:55:56 -04:00
|
|
|
driver := &Workstation9Driver{}
|
|
|
|
|
|
|
|
vmnetnat := driver.VmnetnatConfPath()
|
|
|
|
if vmnetnat == "" {
|
2013-08-09 20:31:43 -04:00
|
|
|
return "", errors.New("Could not find NAT vmnet conf file")
|
2013-07-31 17:46:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := os.Stat(vmnetnat); err != nil {
|
2013-08-09 20:31:43 -04:00
|
|
|
return "", fmt.Errorf("Could not find NAT vmnet conf file: %s", vmnetnat)
|
2013-07-31 17:46:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
f, err := os.Open(vmnetnat)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
ipRe := regexp.MustCompile(`^\s*ip\s*=\s*(.+?)\s*$`)
|
|
|
|
|
|
|
|
r := bufio.NewReader(f)
|
|
|
|
for {
|
|
|
|
line, err := r.ReadString('\n')
|
|
|
|
if line != "" {
|
|
|
|
matches := ipRe.FindStringSubmatch(line)
|
|
|
|
if matches != nil {
|
|
|
|
ip := matches[1]
|
|
|
|
dotIndex := strings.LastIndex(ip, ".")
|
|
|
|
if dotIndex == -1 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
ip = ip[0:dotIndex] + ".1"
|
|
|
|
return ip, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2013-07-31 18:10:28 -04:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2013-07-31 17:46:12 -04:00
|
|
|
}
|
|
|
|
|
2013-08-02 02:55:56 -04:00
|
|
|
return "", errors.New("host IP not found in " + vmnetnat)
|
2013-07-31 17:46:12 -04:00
|
|
|
}
|