How do I get the IPaddress of the current machine from Bash in a cross platform compatible way? I need to get the IP address the same way for Windows Linux and Mac OS.
I am currently using docker-compose
to create a local version of my full deployment, however I can't access it using localhost or 127.0.0.1, I have to refer to the current machines IP address, for example curl 192.168.0.23:80
Currently I make the user set the IP address manually:
# Return true if we pass in an IPv4 pattern.
not_ip() {
rx='([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])'
if [[ $1 =~ ^$rx\.$rx\.$rx\.$rx$ ]]; then
return 1
else
return 0
fi
}
# Ensure lower case
OPTION=$(echo $1 | tr [:upper:] [:lower:])
case "$OPTION" in
"test")
LOCAL_IP=${LOCAL_IP:-$2}
if not_ip "$LOCAL_IP" ; then
echo The test command couldn\'t resolve your computers Network IP. $LOCAL_IP
echo
help_comment
exit 1
fi
python -m webbrowser "http://${LOCAL_IP}:80/" &
;;
esac
However I would love to be able to get this without having to have the user set any environment variables, especially when dealing with Windows machines.
Any ideas?