summary refs log tree commit diff
path: root/geolocate
diff options
context:
space:
mode:
Diffstat (limited to 'geolocate')
-rwxr-xr-xgeolocate77
1 files changed, 77 insertions, 0 deletions
diff --git a/geolocate b/geolocate
new file mode 100755
index 0000000..b11846f
--- /dev/null
+++ b/geolocate
@@ -0,0 +1,77 @@
+#!/bin/bash
+
+# Check for an internet connection
+echo -e "GET http://google.com HTTP/1.0\n\n" | nc google.com 80 > /dev/null 2>&1
+
+if [ $? -eq 0 ]; then
+    :
+else
+    echo "You are offline, this script requires an internet connection."
+    exit 1
+fi
+
+usage() {
+  echo -e "Usage: 'geolocate COMMAND'\n"
+  echo -e "A script to obtain the approximate latitude and longitude given an ip adress.\n"
+  echo -e "Commands:"
+  echo -e "  help       - print a summary of the script's function and usage."
+  echo -e "  version  - print the scripts version."
+  echo -e "  ip:"
+  echo -e "     ip 'blank' - print your public ip adress."
+  echo -e "     ip 'IP'    - get coordinates of a non-personal ip."
+}
+
+getIp() {
+  PUBLIC_IP=`curl -s https://ipinfo.io/ip`
+  echo ${PUBLIC_IP}
+}
+
+getCoord() {
+curl -s https://ipvigilante.com/ | \
+        jq '.data.latitude, .data.longitude' | \
+        while read -r LATITUDE; do
+                read -r LONGITUDE
+                echo "${LATITUDE}:${LONGITUDE}" | tr -d '"'
+        done
+}
+
+        main() {
+
+            case "$1" in
+            --help|-h|help|h)
+                usage
+                exit 0
+                ;;
+            version)
+                version
+                exit 0
+                ;;
+            ip|i)
+                case "$2" in
+                  "")
+                      getIp
+                      exit 0
+                      ;;
+                  *)
+                      IP="$2"
+                      curl -s https://ipvigilante.com/$IP | \
+                              jq '.data.latitude, .data.longitude' | \
+                              while read -r LATITUDE; do
+                                      read -r LONGITUDE
+                                      echo "${LATITUDE}:${LONGITUDE}" | tr -d '"'
+                              done
+                      exit 0
+                      ;;
+                  esac
+                  exit 0
+                ;;
+            *)
+                getCoord
+                exit 0
+                ;;
+            esac
+            exit 0
+        }
+
+        # call main function
+        main "${@}"