-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathext_ip
executable file
·55 lines (46 loc) · 1.74 KB
/
ext_ip
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#! /bin/bash
# Print externally visible IPv4 and IPv6 addresses.
#
# Copyright (c) 2013-2025 Erik Auerswald <[email protected]>
#
# Copying and distribution of this file, with or without modification,
# are permitted in any medium without royalty provided the copyright
# notice and this notice are preserved. This file is offered as-is,
# without any warranty.
set -u
HAVE_LYNX=''
HAVE_WGET=''
declare -a LYNX WGET
# use lynx to create text from HTML
if command -v lynx >/dev/null; then
LYNX=('lynx' '-dump' '-nolist')
HAVE_LYNX='lynx'
WGET=('lynx' '-dump' '-nolist')
HAVE_WGET='lynx'
fi
# DynDNS and icanhazip work with wget and curl, too
if command -v wget >/dev/null; then
WGET=('wget' '-qO-')
HAVE_WGET='wget'
elif command -v curl >/dev/null; then
WGET=('curl' '-s')
HAVE_WGET='curl'
fi
# get the current IPv4 address using DynDNS (returns HTML)
IPv4=''
test -n "$HAVE_WGET" && IPv4=$("${WGET[@]}" http://checkip.dyndns.com/ 2> /dev/null | \
sed -nE 's/^.*[^0-9]([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}).*$/\1/p'&)
# transparently get the IPv6 or IPv4 address using icanhazip (returns text)
IPv46=''
test -n "$HAVE_WGET" && IPv46=$("${WGET[@]}" https://icanhazip.com/ \
2> /dev/null | sed -nE 's/^[[:space:]]*([0-9a-f:.]+)[[:space:]]*$/\1/p'&)
# get the current IPv6 address using DynDNS (returns HTML)
IPv6=''
test -n "$HAVE_WGET" && IPv6=$("${WGET[@]}" http://checkipv6.dyndns.com/ \
2> /dev/null | sed -nE 's/^.*Address: ([0-9a-f:]+).*$/\1/p'&)
wait
test -n "$IPv4" && echo "IPv4 address (DynDNS): $IPv4"
test -n "$IPv6" && echo "IPv6 address (DynDNS): $IPv6"
test -n "$IPv46" && echo "preferred Dual-Stack address (icanhazip): $IPv46"
test -z "$IPv4" && test -z "$IPv46" && test -z "$IPv6" && exit 1
exit 0