-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathipinfo
executable file
·61 lines (47 loc) · 1.98 KB
/
ipinfo
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
56
57
58
59
60
61
#!/usr/bin/env bash
# More safety by turning bugs into errors.
# Use `${PIPESTATUS[n]}` to check for the `n`-th pipe command's exit status.
# Make sure there are no uninitialized variables.
# To allow a non-zero exit status or to allow a pipe to fail without exiting
# the script, prefix the command with `!`.
# Option `noclobber` makes sure piping into a file with `>` does not
# override existing files. Use `>|` to overwrite files instead.
# Environment variables are `UPPER_SNAKE_CASE`, global constants are
# `_UNDERSCORE_PREFIXED` and global variables are `_underscore_prefixed`
set -o errexit -o pipefail -o nounset -o noclobber
: ${XDG_CONFIG_HOME:=$HOME/.config}
: ${XDG_CACHE_HOME:=$HOME/.cache}
readonly _SCRIPT="$(realpath "$0")"
readonly _SCRIPT_NAME="${_SCRIPT##*/}"
readonly _SCRIPT_DIR="${_SCRIPT%/*}"
err() { echo >&2 "${_SCRIPT_NAME}: $@"; }
readonly _ERR_USAGE=2
readonly _ERR_PROGRAMMING=255
readonly _USAGE="
Usage:
$_SCRIPT_NAME [options] [api-path]
OPTIONS:
-h, --help Show this help message.
ARGUMENTS:
api-path Path added to the API URL like so: \`https://ipinfo.io/<api-url>\`
Examples:
- ip - Get current ip as plaintext
- 1.1.1.1 - Get ipinfo on 8.8.8.8
- 8.8.8.8/org - Get paintext organization of 8.8.8.8
- 2620:fe::9 - Get ipinfo on an IPv6
"
readonly _OPTIONS='h'
readonly _LONGOPTS='help'
parsed="$(getopt -o "$_OPTIONS" -l "$_LONGOPTS" -n "$_SCRIPT_NAME" -- "$@")"
if [[ ${PIPESTATUS[0]} -ne 0 ]]; then echo >&2 "$_USAGE"; exit $_ERR_USAGE; fi
eval set -- "${parsed[@]}"; unset parsed
while [[ $# -gt 0 ]]; do
case $1 in
-h|--help) echo "$_USAGE"; exit; ;;
--) shift; break; ;;
*) err "Programming error"; exit $_ERR_PROGRAMMING; ;;
esac
done
# --- main program -------------------------------------------------------------
url_path=${1:-''}
curl -s "https://ipinfo.io/${url_path}"