mosys0815 commented on code in PR #11800:
URL: https://github.com/apache/cloudstack/pull/11800#discussion_r2536785396


##########
utils/src/main/java/com/cloud/utils/net/NetUtils.java:
##########
@@ -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'");

Review Comment:
   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.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to