Skip to content

Commit

Permalink
adding in ip enumeration for wsl/hyper-v (#1539)
Browse files Browse the repository at this point in the history
  • Loading branch information
eyberg authored Nov 7, 2023
1 parent d2ac116 commit 21b5478
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
*.swp
*.pb.go
*.pb.gw.go
*.swagger.json
Expand Down
24 changes: 24 additions & 0 deletions provider/hyperv/hyperv_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package hyperv

import (
"fmt"
"strconv"
"strings"
)
Expand Down Expand Up @@ -32,6 +33,29 @@ func GetVirtualMachineByName(vmName string) (string, error) {
return cmdOut, err
}

func getM2IP() map[string]string {
var script = `Get-NetNeighbor -LinkLayerAddress 00-15-5d-* | Select IPAddress, LinkLayerAddress`

var m = make(map[string]string)

var ps PowerShellCmd
cmdOut, err := ps.Output(script)
if err != nil {
fmt.Println(err)
}

lines := strings.Split(cmdOut, "\r\n")
for i := 2; i < len(lines); i++ {
cols := strings.Split(lines[i], " ")
if len(cols) > 1 {

m[cols[0]] = cols[1]
}
}

return m
}

// GetVirtualMachineNetworkAdapterAddress returns a virtual machine ip address
func GetVirtualMachineNetworkAdapterAddress(vmName string) (string, error) {

Expand Down
9 changes: 9 additions & 0 deletions provider/hyperv/hyperv_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package hyperv

import (
"encoding/json"
"errors"
"fmt"
"os"
Expand Down Expand Up @@ -107,6 +108,14 @@ func (p *Provider) ListImages(ctx *lepton.Context) error {
return err
}

if ctx.Config().RunConfig.JSON {
if len(images) == 0 {
fmt.Println("[]")
return nil
}
return json.NewEncoder(os.Stdout).Encode(images)
}

table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Name", "Path", "Size", "CreatedAt"})
table.SetHeaderColor(
Expand Down
35 changes: 30 additions & 5 deletions provider/hyperv/hyperv_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package hyperv

import (
"bufio"
"encoding/json"
"errors"
"fmt"
"math"
Expand Down Expand Up @@ -90,6 +91,14 @@ func (p *Provider) ListInstances(ctx *lepton.Context) error {
return err
}

if ctx.Config().RunConfig.JSON {
if len(instances) == 0 {
fmt.Println("[]")
return nil
}
return json.NewEncoder(os.Stdout).Encode(instances)
}

table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Name", "Status", "Created", "Private Ips", "Port"})
table.SetHeaderColor(
Expand Down Expand Up @@ -134,25 +143,41 @@ func (p *Provider) GetInstances(ctx *lepton.Context) (instances []lepton.CloudIn

if len(resp) > 2 {

ips := getM2IP()

for i := 2; i < len(resp); i++ {
line := resp[i]

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

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

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

ip := ""

m, err := Mac(cols[0])
if err != nil {
fmt.Println(err)
}

for k, v := range ips {
rv := strings.ReplaceAll(v, "-", "")
if rv == m {
ip = k
}
}

instances = append(instances, lepton.CloudInstance{
Name: cols[0],
Status: cols[1],
Created: lepton.Time2Human(date),
Name: cols[0],
Status: cols[1],
PrivateIps: []string{ip},
Created: lepton.Time2Human(date),
})
}

Expand Down

0 comments on commit 21b5478

Please sign in to comment.