Commits

rsvp committed d3578a6 Draft

Update digy with geo feature using ip2geo.

Comments (0)

Files changed (2)

 #!/usr/bin/env bash
-#              bash 4.1.5(1)     Linux Ubuntu 10.04           Date : 2012-02-17
+#              bash 4.1.5(1)     Linux Ubuntu 10.04           Date : 2012-06-17
 #
 # _______________|  digy : tame DNS utility dig with reasonable options.
 #
-#           Usage:  digy  [address]  [rev|name|short|mail|all|long|longa|who]
+#           Usage:  digy  [address]  [rev|name|short|mail|all|long|longa|who|geo]
 #
 #        Examples:     % digy friendfeed.com
 #                      69.63.180.52
 #                      #  default second argument is 'short'
 #                      #
-#                      #       With no arguments whatsoever, digy outputs 
+#                      #       With no arguments whatsoever, "digy" outputs 
 #                      #       your public WAN IP address.
+#                      #       Your public geo coordinates: "digy igeo"
 #                      #       For LAN info: "digy local"
 #  
 #                      % digy 69.63.180.52 rev
 #                      Menlo Park CA 94025
 #                      domain@fb.com +1.6505434800 Fax: +1.6505434800
 #                      # output was edited for this example of 'who'.
+#
+#                      % digy 69.63.180.52 geo
+#                      69.63.180.52 37.4429016113 -122.151397705 US CA Palo Alto
+#                      #  IP, latitude, longitude, country, region, locality
 #  
 #
 #    Dependencies:  dig   (standard issue from package 'dnsutils' (main))
 #                         [assuming herein non-existence of $HOME/.digrc]
 #                   curl  (only to discern your own public IP address)
 #                   whois (with sed and awk filtering).
+#                   ip2geo (from my gists, to get geographic coordinates).
 
 #  CHANGE LOG  LATEST version available:   https://bitbucket.org/rsvp/gists/src
 #
+#  2012-06-17  Add geo option to get geographic coordinates.
 #  2012-02-17  Add convenience option 'local' as first argument for address 
 #                 which gives LAN information. Revise preamble.
 #  2012-02-15  Second argument 'who' yields non-verbose domain name 
                #    Then the name is revealed by the rest of this script.
                opt='rev'                                                     ;;
 
+      'igeo')  addr="$( get_publicIP )"
+               ip2geo $addr
+               #      Where am I geographically from public perspective? 
+               cleanup                                                       ;;
+
     '--help')  more "$0"  &&  cleanup                                        ;;
      '-h')     more "$0"  &&  cleanup                                        ;;
                #    source is the best documentation :-)
                #          awk takes the cleansed "paragraph" as record, 
                #          and pulls out those involving CONTACT or DATE.
 
+       'geo')  ip2geo $addr                                                  ;;
+
            *)  die "undefined second arg: $opt" 113                          ;;
 esac > $memf   
 
 #
 # _______________|  ip2geo : given IP, get latitude/longitude, country, locality.
 #
-#           Usage:  ip2geo
+#           Usage:  ip2geo [IP numerical, or domain]
 #
 #        Examples:  % ip2geo  130.12.1.34%2C67.169.73.113
 #                   #         Officially IP arguments are expected as a list.
 
 for ip in data:
      #    ^now is a Python dictionary.
-     print ip, data[ip]['latitude'], data[ip]['longitude'],                  \
-           data[ip]['country_code'], data[ip]['region'], data[ip]['locality']
+     if not data[ip]:
+          print ip, "999 999 XX XX NULL invalid IP"
+          #     output something since the other IPs might be valid.
+     else:
+          print ip, data[ip]['latitude'], data[ip]['longitude'],                  \
+                data[ip]['country_code'], data[ip]['region'], data[ip]['locality']
 
 EOHereDoc
-}  
+}  2> /dev/null  ||  die 'invalid IP address? Python json module choked.'  117