Browse Source

Add shell helper to get current public IP

Emmanuel Bouthenot 9 years ago
parent
commit
56295db3b6
1 changed files with 27 additions and 0 deletions
  1. 27 0
      .zsh/config/10_alias.zsh

+ 27 - 0
.zsh/config/10_alias.zsh

@@ -157,3 +157,30 @@ http_auth_ssha() {
     printf "%s:{SSHA}%s\n" "${username}" "${sha1}"
 }
 
+#
+# DNS stuff
+#
+myip () {
+    # Get public IP address from opendns
+    ipv4=
+    ipv6=
+    ip4_resolver="resolver1.opendns.com"
+    ip6_resolver="2620:0:ccc::2"
+    if (( $+commands[dig] )); then
+        ipv4=$(dig +short myip.opendns.com "@${ip4_resolver}")
+        ipv6=$(dig AAAA +short myip.opendns.com "@${ip6_resolver}")
+    elif (( $+commands[host] )); then
+        ipv4=$(LANG=C host -t A myip.opendns.com "${ip4_resolver}" | sed -r -n 's/^.* has address (.*)$/\1/p')
+        ipv6=$(LANG=C host -t AAAA myip.opendns.com "${ip6_resolver}" | sed -r -n 's/^.* has IPv6 address (.*)$/\1/p')
+    fi
+    if [[ -n "${ipv4}" ]]; then
+        printf "IPv4 is %s\n" "${ipv4}"
+    else
+        printf "IPv4 not found\n" >&2
+    fi
+    if [[ -n "${ipv6}" ]]; then
+        printf "IPv6 is %s\n" "${ipv6}"
+    else
+        printf "IPv6 not found\n" >&2
+    fi
+}