2018-06-22 18:18:09 -07:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
function myip()
|
|
|
|
{
|
2018-06-25 11:13:41 -07:00
|
|
|
# shellcheck disable=SC2207
|
|
|
|
declare ipaddrs=(
|
|
|
|
# query interwebs
|
|
|
|
$(curl -s ifconfig.co)
|
|
|
|
# machine's interfaces
|
|
|
|
$(ifconfig |
|
|
|
|
awk '/inet addr:/ {gsub("addr:","",$2); print $2; next}
|
|
|
|
/inet6 addr:/ {gsub("/.*", "", $3); print $3; next}
|
|
|
|
/inet(6)? / {print $2}'
|
|
|
|
)
|
|
|
|
)
|
2018-06-22 18:18:09 -07:00
|
|
|
|
|
|
|
if (( ! ${#ipaddrs[*]} ))
|
|
|
|
then
|
|
|
|
echo "
|
|
|
|
myip: error: I'm having trouble determining what our IP address is...
|
|
|
|
Are we connected to a network?
|
|
|
|
|
|
|
|
"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
|
|
declare prompt="
|
|
|
|
Please choose the IP address you want to advertise to the network:
|
|
|
|
|
|
|
|
0) ${ipaddrs[0]} <====== this one was returned by the interwebs...
|
|
|
|
"
|
|
|
|
|
|
|
|
for ((i=1; i < ${#ipaddrs[*]}; i++))
|
|
|
|
do
|
|
|
|
prompt+=" $i) ${ipaddrs[i]}
|
|
|
|
"
|
|
|
|
done
|
|
|
|
|
2018-06-23 11:52:12 -07:00
|
|
|
while read -r -p "${prompt}
|
2018-06-22 18:18:09 -07:00
|
|
|
please enter a number [0 for default]: " which
|
|
|
|
do
|
|
|
|
[[ -z ${which} ]] && break;
|
|
|
|
[[ ${which} =~ [0-9]+ ]] && (( which < ${#ipaddrs[*]} )) && break;
|
|
|
|
echo "Ug. invalid entry \"${which}\"...
|
|
|
|
"
|
|
|
|
sleep 1
|
|
|
|
done
|
|
|
|
|
|
|
|
which=${which:-0}
|
|
|
|
|
|
|
|
echo "${ipaddrs[which]}"
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-06-23 11:52:12 -07:00
|
|
|
if [[ ${0} == "${BASH_SOURCE[0]}" ]]
|
2018-06-22 18:18:09 -07:00
|
|
|
then
|
|
|
|
myip "$@"
|
|
|
|
fi
|