Scripts: update myip script for wan
This commit is contained in:
parent
e11cef88d2
commit
210054fe8b
1 changed files with 14 additions and 56 deletions
70
bin/myip
70
bin/myip
|
@ -2,66 +2,24 @@
|
|||
# List system's WAN and LAN ip addresses
|
||||
# Depends on ifconfig and awk
|
||||
|
||||
if [[ $1 =~ '-h' ]]; then
|
||||
cat <<EOF
|
||||
List the system's WAN and LAN addresses
|
||||
USAGE:
|
||||
$(basename "$0")
|
||||
EOF
|
||||
exit 0
|
||||
fi
|
||||
declare -r ipv4="$(curl --max-time 1 -s https://api4.ipify.org)"
|
||||
|
||||
|
||||
awk_lan () {
|
||||
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]'
|
||||
if [[ -n "$ipv4" ]]; then
|
||||
echo "IPv4 (WAN): $ipv4"
|
||||
else
|
||||
echo '`ifconfig` not found'
|
||||
echo "IPv4 (WAN): Not available"
|
||||
fi
|
||||
|
||||
# Print network's public IP address
|
||||
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
|
||||
declare -r ipv6="$(curl --max-time 1 -s https://api6.ipify.org)"
|
||||
|
||||
# Backup address fetching. Slower though more reliable
|
||||
if [[ -z $public_ip ]]; then
|
||||
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))
|
||||
}'
|
||||
if [[ -n "$ipv6" ]]; then
|
||||
echo "IPv6 (WAN): $ipv6"
|
||||
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
|
||||
|
|
Loading…
Reference in a new issue