forked from hashicorp/packer
/
host_ip_ifconfig.go
101 lines (83 loc) · 2.28 KB
/
host_ip_ifconfig.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package common
import (
"bytes"
"errors"
"os"
"os/exec"
"regexp"
)
// IfconfigIPFinder finds the host IP based on the output of `ip address` or `ifconfig`.
type IfconfigIPFinder struct {
Device string
}
func (f *IfconfigIPFinder) HostIP() (string, error) {
ip, err := ipaddress(f.Device)
if err != nil || ip == "" {
return ifconfig(f.Device)
}
return ip, err
}
func ipaddress(device string) (string, error) {
var ipPath string
// On some systems, ip is in /sbin which is generally not
// on the PATH for a standard user, so we just check that first.
if _, err := os.Stat("/sbin/ip"); err == nil {
ipPath = "/sbin/ip"
}
if ipPath == "" {
var err error
ipPath, err = exec.LookPath("ip")
if err != nil {
return "", err
}
}
stdout := new(bytes.Buffer)
cmd := exec.Command(ipPath, "address", "show", "dev", device)
// Force LANG=C so that the output is what we expect it to be
// despite the locale.
cmd.Env = append(cmd.Env, "LANG=C")
cmd.Env = append(cmd.Env, os.Environ()...)
cmd.Stdout = stdout
cmd.Stderr = new(bytes.Buffer)
if err := cmd.Run(); err != nil {
return "", err
}
re := regexp.MustCompile(`inet[^\d]+([\d\.]+)/`)
matches := re.FindStringSubmatch(stdout.String())
if matches == nil {
return "", errors.New("IP not found in ip a output...")
}
return matches[1], nil
}
func ifconfig(device string) (string, error) {
var ifconfigPath string
// On some systems, ifconfig is in /sbin which is generally not
// on the PATH for a standard user, so we just check that first.
if _, err := os.Stat("/sbin/ifconfig"); err == nil {
ifconfigPath = "/sbin/ifconfig"
}
if ifconfigPath == "" {
var err error
ifconfigPath, err = exec.LookPath("ifconfig")
if err != nil {
return "", err
}
}
stdout := new(bytes.Buffer)
cmd := exec.Command(ifconfigPath, device)
// Force LANG=C so that the output is what we expect it to be
// despite the locale.
cmd.Env = append(cmd.Env, "LANG=C")
cmd.Env = append(cmd.Env, os.Environ()...)
cmd.Stdout = stdout
cmd.Stderr = new(bytes.Buffer)
if err := cmd.Run(); err != nil {
return "", err
}
re := regexp.MustCompile(`inet[^\d]+([\d\.]+)\s`)
matches := re.FindStringSubmatch(stdout.String())
if matches == nil {
return "", errors.New("IP not found in ifconfig output...")
}
return matches[1], nil
}