diff --git a/pkg/util/netsh/netsh.go b/pkg/util/netsh/netsh.go index c3b963c411b..30b66536b1d 100644 --- a/pkg/util/netsh/netsh.go +++ b/pkg/util/netsh/netsh.go @@ -190,9 +190,8 @@ func checkIPExists(ipToCheck string, args []string, runner *runner) (bool, error glog.V(3).Infof("Searching for IP: %v in IP dump: %v", ipToCheck, ipAddressString) showAddressArray := strings.Split(ipAddressString, "\n") for _, showAddress := range showAddressArray { - if strings.Contains(showAddress, "IP Address:") { - ipFromNetsh := strings.TrimLeft(showAddress, "IP Address:") - ipFromNetsh = strings.TrimSpace(ipFromNetsh) + if strings.Contains(showAddress, "IP") { + ipFromNetsh := getIP(showAddress) if ipFromNetsh == ipToCheck { return true, nil } @@ -201,3 +200,12 @@ func checkIPExists(ipToCheck string, args []string, runner *runner) (bool, error return false, nil } + +// getIP gets ip from showAddress (e.g. "IP Address: 10.96.0.4"). +func getIP(showAddress string) string { + list := strings.SplitN(showAddress, ":", 2) + if len(list) != 2 { + return "" + } + return strings.TrimSpace(list[1]) +} diff --git a/pkg/util/netsh/netsh_test.go b/pkg/util/netsh/netsh_test.go index ac832b72a54..2194c8318ea 100644 --- a/pkg/util/netsh/netsh_test.go +++ b/pkg/util/netsh/netsh_test.go @@ -438,3 +438,30 @@ func TestCheckIPExists(t *testing.T) { } } } + +func TestGetIP(t *testing.T) { + testcases := []struct { + showAddress string + expectAddress string + }{ + { + showAddress: "IP 地址: 10.96.0.2", + expectAddress: "10.96.0.2", + }, + { + showAddress: "IP Address: 10.96.0.3", + expectAddress: "10.96.0.3", + }, + { + showAddress: "IP Address:10.96.0.4", + expectAddress: "10.96.0.4", + }, + } + + for _, tc := range testcases { + address := getIP(tc.showAddress) + if address != tc.expectAddress { + t.Errorf("expected address=%q, got %q", tc.expectAddress, address) + } + } +}