I'm trying to extract the local IP address using a cross-platform command. Until today, I was using this command:
ip route get 1 | awk '{print $NF;exit}'
But on Fedora 27 is not working because the output of ip route get 1
is:
0.0.0.1 via 192.168.1.1 dev en1 src 192.168.0.229 uid 1000
cache
And I'm getting 1000
as the IP address. In all other systems that I have tried, the output has been always:
0.0.0.1 via 192.168.1.1 dev en1 src 192.168.0.229
I also tried using this command with same result:
ip route get 255.255.255.255 | sed -n '/src/ s/.*src //p'
To print the address coming just after src
(assuming all the relevant parts stay on the same line...):
ip route get 1 | sed 's/^.*src \([^ ]*\).*$/\1/;q'
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加