From 5aea62e8cf201587b771f72bdf9f22bca4b9b8c7 Mon Sep 17 00:00:00 2001
From: Adar Nimrod <nimrod@shore.co.il>
Date: Sat, 27 May 2023 17:48:17 +0300
Subject: [PATCH] ipinfo script.

Get IP info from ipinfo.io.
---
 .bashrc              |  2 +-
 Documents/bin/ipinfo | 36 ++++++++++++++++++++++++++++++++++++
 2 files changed, 37 insertions(+), 1 deletion(-)
 create mode 100755 Documents/bin/ipinfo

diff --git a/.bashrc b/.bashrc
index 9d9b6b5..284a311 100644
--- a/.bashrc
+++ b/.bashrc
@@ -231,7 +231,7 @@ gen_csr () {
 jt () {
     if command -v pygmentize > /dev/null
     then
-        python3 -m json.tool "$@" | pygmentize -l javascript
+        python3 -m json.tool "$@" | pygmentize -l json
     else
         python3 -m json.tool "$@"
     fi
diff --git a/Documents/bin/ipinfo b/Documents/bin/ipinfo
new file mode 100755
index 0000000..b7a96f7
--- /dev/null
+++ b/Documents/bin/ipinfo
@@ -0,0 +1,36 @@
+#!/bin/sh
+set -eu
+
+is_ip () {
+    echo "$1" | grep --extended-regexp --quiet  '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}'
+}
+
+usage () {
+    echo "USAGE: $(basename "$0") [-h] [hostname or IP address]" >&2
+    echo "If no address is passed, the internet IP address is used instead." >&2
+}
+
+if [ "$#" -eq 0 ]
+then
+    address="$(curl --silent --fail --show-error https://myip.shore.co.il/)"
+elif [ "$#" -gt 1 ]
+then
+    usage
+    exit 1
+elif [ "$1" = "-h" ]
+then
+    usage
+elif is_ip "$1"
+then
+    address="$1"
+else
+    address="$(dig +short "$1")"
+fi
+
+
+if command -v pygmentize > /dev/null
+then
+    curl --silent --fail --show-error --header "Accept: application/json" "https://ipinfo.io/${address}" | pygmentize -l json
+else
+    curl --fail --show-error --header "Accept: application/json" "https://ipinfo.io/${address}"
+fi
-- 
GitLab