From 1349fb8f56b2cca157b8a87644fdf50bcca93689 Mon Sep 17 00:00:00 2001 From: mchtech Date: Mon, 6 Aug 2018 01:47:03 +0800 Subject: [PATCH] Fix for wrongly matching IPv6 address line problem in GetNicNameByIp --- src/zvr/utils/net.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/zvr/utils/net.go b/src/zvr/utils/net.go index b73d0365..e3d07dd2 100755 --- a/src/zvr/utils/net.go +++ b/src/zvr/utils/net.go @@ -119,8 +119,11 @@ func GetNicNameByMac(mac string) (string, error) { } func GetNicNameByIp(ip string) (string, error) { + // Modify IPv4 regex pattern from "a.b.c.d" to "a\.b\.c\.d" to avoid matching the line + // that contains IPv6 address, of which pattern is like 2001:db8::a:b:c:d + ipv4regex := strings.Replace(ip, ".", `\.`, -1) bash := Bash{ - Command: fmt.Sprintf("ip addr | grep -w %s", ip), + Command: fmt.Sprintf("ip addr | grep -w '%s'", ipv4regex), } ret, o, _, err := bash.RunWithReturn() if err != nil {