Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion utils/src/main/java/com/cloud/utils/net/NetUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ public static String getDefaultEthDevice() {
final String defDev = Script.runSimpleBashScript("/sbin/route -n get default 2> /dev/null | grep interface | awk '{print $2}'");
return defDev;
}
return Script.runSimpleBashScript("ip route show default 0.0.0.0/0 | head -1 | awk '{print $5}'");
return Script.runSimpleBashScript("ip -j a | jq -r '.[] | .addr_info | map(select(.local == \"'`ip -j r s default | jq -r '.[0] | .prefsrc'`'\")) | .[].label'");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return Script.runSimpleBashScript("ip -j a | jq -r '.[] | .addr_info | map(select(.local == \"'`ip -j r s default | jq -r '.[0] | .prefsrc'`'\")) | .[].label'");
return Script.runSimpleBashScript("ip route show default | awk '{print $5}'");

My suggestion.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return Script.runSimpleBashScript("ip -j a | jq -r '.[] | .addr_info | map(select(.local == \"'`ip -j r s default | jq -r '.[0] | .prefsrc'`'\")) | .[].label'");
return Script.runSimpleBashScript("ip route show default | awk '{print $5}'");

My suggestion.

agree with it

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return Script.runSimpleBashScript("ip -j a | jq -r '.[] | .addr_info | map(select(.local == \"'`ip -j r s default | jq -r '.[0] | .prefsrc'`'\")) | .[].label'");
return Script.runSimpleBashScript("ip route show default | awk '{print $5}'");

My suggestion.

agree with it

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your feedback @daviftorres :)

Using json to retrieve structured data makes plain text parsing unnecessary, it does not matter how the text output is formatted and how the content differs in the future.

Tbh i am not quite sure whether jq is available in a minimal installation, but i wouldn't see it as a big deal or security relevant dependency.

For testing purposes you can not just copy the script into your console. I had to escape and quote parts additionally because of the general quoting in Script.runSimpleBashScript. The End result, which you also can see in debug output of management server log looks like:

ip -j a | jq -r '.[] | .addr_info | map(select(.local == "'`ip -j r s default | jq -r '.[0] | .prefsrc'`'")) | .[].label'

To show you the output of your suggestion on our l3 network:

~$ ip route show default
default proto bird src 10.64.16.3 metric 32
	nexthop via inet6 fe80::9e5a:80ff:feaf:b108 dev eth1a weight 1
	nexthop via inet6 fe80::e65e:ccff:fe44:1a08 dev eth1b weight 1
default via 10.64.17.229 dev eth1b proto static src 10.64.17.230 metric 1024 onlink
default via 10.64.16.229 dev eth1a proto static src 10.64.16.230 metric 1024 onlink
~$ ip route show default | awk '{print $5}'
10.64.16.3
dev
dev
eth1b
eth1a

That is nothing different from where I come from, hence proposing my change.
Using json structured data and filtering for "label" in "prefsrc" always gives the correct default outgoing interface.

Your json variant

~$ ip -j r s default | jq -r '.[0] | .dev'
null

unfortunately does not return any interface at all.

}

public static String getLocalIPString() {
Expand Down
Loading