Scripts: update myip script for wan

This commit is contained in:
Akemi Izuko 2024-05-08 19:54:51 -06:00
parent e11cef88d2
commit 210054fe8b
Signed by: akemi
GPG key ID: 8DE0764E1809E9FC

View file

@ -2,66 +2,24 @@
# List system's WAN and LAN ip addresses # List system's WAN and LAN ip addresses
# Depends on ifconfig and awk # Depends on ifconfig and awk
if [[ $1 =~ '-h' ]]; then declare -r ipv4="$(curl --max-time 1 -s https://api4.ipify.org)"
cat <<EOF
List the system's WAN and LAN addresses
USAGE:
$(basename "$0")
EOF
exit 0
fi
if [[ -n "$ipv4" ]]; then
awk_lan () { echo "IPv4 (WAN): $ipv4"
awk '
BEGIN { print "Local:" }
/UP/ {
split($0, info, /:/)
device_name = '"$1"'
is_up = 1
}
match($0, /inet ([0-9]{1,3}\.){3}[0-9]{1,3}(\/[0-9]{1,3})? /) {
if (is_up) {
ip_addr = substr($0, RSTART + 5, RLENGTH - 5)
printf(" %s: %s\n", device_name, ip_addr)
is_up = 0
}
}' -
}
# List all possible inet addresses
if command -v ip address &> /dev/null; then
ip address | awk_lan 'substr(info[2], 2)'
elif command -v ifconfig &> /dev/null; then
ifconfig | awk_lan 'info[1]'
else else
echo '`ifconfig` not found' echo "IPv4 (WAN): Not available"
fi fi
# Print network's public IP address declare -r ipv6="$(curl --max-time 1 -s https://api6.ipify.org)"
if command -v dig &> /dev/null; then
public_ip=$(dig +short myip.opendns.com @resolver2.opendns.com)
elif command -v host &> /dev/null; then
public_ip=$(host myip.opendns.com resolver1.opendns.com)
fi
# Backup address fetching. Slower though more reliable if [[ -n "$ipv6" ]]; then
if [[ -z $public_ip ]]; then echo "IPv6 (WAN): $ipv6"
if command -v curl &> /dev/null; then
public_ip=$(curl --silent https://ifconfig.me/ip)
elif command -v wget &> /dev/null; then
public_ip=$(wget --quiet -O - https://ifconfig.me/ip)
fi
fi
if [[ -n $public_ip ]]; then
echo "$public_ip" | awk ' match($0, /([0-9]{1,3}\.){3}[0-9]{1,3}$/) {
printf("Public (WAN): %s\n", substr($0, RSTART, RLENGTH))
}'
else else
echo 'Public IP not found' echo "IPv6 (WAN): Not available"
fi
declare -r ipv64="$(curl --max-time 1 -s https://api64.ipify.org)"
if [[ -n "$ipv64" ]]; then
echo "First Resolved: $ipv64"
fi fi