Skip to content

Commit 21b5478

Browse files
authored
adding in ip enumeration for wsl/hyper-v (#1539)
1 parent d2ac116 commit 21b5478

File tree

4 files changed

+64
-5
lines changed

4 files changed

+64
-5
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
*.swp
12
*.pb.go
23
*.pb.gw.go
34
*.swagger.json

provider/hyperv/hyperv_api.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
package hyperv
44

55
import (
6+
"fmt"
67
"strconv"
78
"strings"
89
)
@@ -32,6 +33,29 @@ func GetVirtualMachineByName(vmName string) (string, error) {
3233
return cmdOut, err
3334
}
3435

36+
func getM2IP() map[string]string {
37+
var script = `Get-NetNeighbor -LinkLayerAddress 00-15-5d-* | Select IPAddress, LinkLayerAddress`
38+
39+
var m = make(map[string]string)
40+
41+
var ps PowerShellCmd
42+
cmdOut, err := ps.Output(script)
43+
if err != nil {
44+
fmt.Println(err)
45+
}
46+
47+
lines := strings.Split(cmdOut, "\r\n")
48+
for i := 2; i < len(lines); i++ {
49+
cols := strings.Split(lines[i], " ")
50+
if len(cols) > 1 {
51+
52+
m[cols[0]] = cols[1]
53+
}
54+
}
55+
56+
return m
57+
}
58+
3559
// GetVirtualMachineNetworkAdapterAddress returns a virtual machine ip address
3660
func GetVirtualMachineNetworkAdapterAddress(vmName string) (string, error) {
3761

provider/hyperv/hyperv_image.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
package hyperv
44

55
import (
6+
"encoding/json"
67
"errors"
78
"fmt"
89
"os"
@@ -107,6 +108,14 @@ func (p *Provider) ListImages(ctx *lepton.Context) error {
107108
return err
108109
}
109110

111+
if ctx.Config().RunConfig.JSON {
112+
if len(images) == 0 {
113+
fmt.Println("[]")
114+
return nil
115+
}
116+
return json.NewEncoder(os.Stdout).Encode(images)
117+
}
118+
110119
table := tablewriter.NewWriter(os.Stdout)
111120
table.SetHeader([]string{"Name", "Path", "Size", "CreatedAt"})
112121
table.SetHeaderColor(

provider/hyperv/hyperv_instance.go

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package hyperv
44

55
import (
66
"bufio"
7+
"encoding/json"
78
"errors"
89
"fmt"
910
"math"
@@ -90,6 +91,14 @@ func (p *Provider) ListInstances(ctx *lepton.Context) error {
9091
return err
9192
}
9293

94+
if ctx.Config().RunConfig.JSON {
95+
if len(instances) == 0 {
96+
fmt.Println("[]")
97+
return nil
98+
}
99+
return json.NewEncoder(os.Stdout).Encode(instances)
100+
}
101+
93102
table := tablewriter.NewWriter(os.Stdout)
94103
table.SetHeader([]string{"Name", "Status", "Created", "Private Ips", "Port"})
95104
table.SetHeaderColor(
@@ -134,25 +143,41 @@ func (p *Provider) GetInstances(ctx *lepton.Context) (instances []lepton.CloudIn
134143

135144
if len(resp) > 2 {
136145

146+
ips := getM2IP()
147+
137148
for i := 2; i < len(resp); i++ {
138149
line := resp[i]
139150

140-
// remove duplicated spaces if they exist
141151
space := regexp.MustCompile(`\s+`)
142152
lineTrimmed := space.ReplaceAllString(line, " ")
143153

144154
cols := strings.Split(lineTrimmed, " ")
145155

146-
date, err := time.Parse("02/01/2006 15:04:05", cols[2]+" "+cols[3])
156+
date, err := time.Parse("01/2/2006 15:04:05", cols[2]+" "+cols[3])
147157
if err != nil {
148158
log.Error(err)
149159
continue
150160
}
151161

162+
ip := ""
163+
164+
m, err := Mac(cols[0])
165+
if err != nil {
166+
fmt.Println(err)
167+
}
168+
169+
for k, v := range ips {
170+
rv := strings.ReplaceAll(v, "-", "")
171+
if rv == m {
172+
ip = k
173+
}
174+
}
175+
152176
instances = append(instances, lepton.CloudInstance{
153-
Name: cols[0],
154-
Status: cols[1],
155-
Created: lepton.Time2Human(date),
177+
Name: cols[0],
178+
Status: cols[1],
179+
PrivateIps: []string{ip},
180+
Created: lepton.Time2Human(date),
156181
})
157182
}
158183

0 commit comments

Comments
 (0)