[profiles/base] change check_ip script
This commit is contained in:
@@ -1,13 +1,14 @@
|
|||||||
#!/usr/bin/env bash
|
#!/bin/bash
|
||||||
|
|
||||||
readonly SCRIPT_NAME=${0##*/}
|
SCRIPT_NAME="${0##*/}"
|
||||||
|
readonly SCRIPT_NAME
|
||||||
|
|
||||||
check_deps() {
|
check_deps() {
|
||||||
command -v curl >/dev/null 2>&1 || die "curl: command not found"
|
command -v curl >/dev/null 2>&1 || die "curl: command not found"
|
||||||
}
|
}
|
||||||
|
|
||||||
usage() {
|
usage() {
|
||||||
cat <<EOF
|
cat <<EOF
|
||||||
Usage: ${SCRIPT_NAME} [--family auto|4|6] [-h|--help]
|
Usage: ${SCRIPT_NAME} [--family auto|4|6] [-h|--help]
|
||||||
|
|
||||||
Query an external service using curl and print:
|
Query an external service using curl and print:
|
||||||
@@ -22,60 +23,66 @@ EOF
|
|||||||
}
|
}
|
||||||
|
|
||||||
shell_quote() {
|
shell_quote() {
|
||||||
local -r string=$1
|
local -r string="$1"
|
||||||
printf "'%s'" "${string//'/'\\''}"
|
printf "'%s'" "${string//'/'\\''/}"
|
||||||
}
|
}
|
||||||
|
|
||||||
die() {
|
die() {
|
||||||
local -r message=$1
|
local -r message="$1"
|
||||||
printf "%s: %b\n" "${SCRIPT_NAME}" "${message}" >&2
|
printf "%s: %b\n" "${SCRIPT_NAME}" "${message}" >&2
|
||||||
exit 1
|
exit 1
|
||||||
}
|
}
|
||||||
|
|
||||||
invalid_option() {
|
invalid_option() {
|
||||||
local -r option=$1
|
local -r option="$1"
|
||||||
die "invalid option $(shell_quote "${option}")\nTry '${SCRIPT_NAME} --help' for usage."
|
die "invalid option $(shell_quote "${option}")
|
||||||
|
Try '${SCRIPT_NAME} --help' for usage."
|
||||||
}
|
}
|
||||||
|
|
||||||
parse_args() {
|
parse_args() {
|
||||||
family="auto"
|
family="auto"
|
||||||
while (( $# > 0 )); do
|
while (($# > 0)); do
|
||||||
case "$1" in
|
case "$1" in
|
||||||
-h|--help) usage; exit 0 ;;
|
-h | --help)
|
||||||
--family)
|
usage
|
||||||
( (( $# < 2 )) || [[ -z "$2" ]] ) && die "--family requires an argument (auto, 4 or 6)"
|
exit 0
|
||||||
family=$2
|
;;
|
||||||
shift 2
|
--family)
|
||||||
;;
|
( (($# < 2)) || [[ -z "$2" ]]) &&
|
||||||
--family=*)
|
die "--family requires an argument (auto, 4 or 6)"
|
||||||
family=${1#*=}
|
family="$2"
|
||||||
[[ -z "${family}" ]] && die "--family requires an argument (auto, 4 or 6)"
|
shift 2
|
||||||
shift
|
;;
|
||||||
;;
|
--family=*)
|
||||||
*) invalid_option "$1" ;;
|
family="${1#*=}"
|
||||||
esac
|
[[ -z "${family}" ]] &&
|
||||||
done
|
die "--family requires an argument (auto, 4 or 6)"
|
||||||
validate_args
|
shift
|
||||||
|
;;
|
||||||
|
*) invalid_option "$1" ;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
validate_args
|
||||||
}
|
}
|
||||||
|
|
||||||
validate_args() {
|
validate_args() {
|
||||||
case ${family} in
|
case "${family}" in
|
||||||
auto|4|6) ;;
|
auto | 4 | 6) ;;
|
||||||
*) die "invalid value for family: '${family}' (expected auto, 4 or 6)"
|
*) die "invalid value for family: '${family}' (expected auto, 4 or 6)" ;;
|
||||||
esac
|
esac
|
||||||
}
|
}
|
||||||
|
|
||||||
show_ip() {
|
show_ip() {
|
||||||
local -a curl_args=()
|
local -a curl_args=()
|
||||||
[[ ${family} == "4" ]] && curl_args=("-4")
|
[[ ${family} == "4" ]] && curl_args=("-4")
|
||||||
[[ ${family} == "6" ]] && curl_args=("-6")
|
[[ ${family} == "6" ]] && curl_args=("-6")
|
||||||
curl "${curl_args[@]}" zx2c4.com/ip
|
curl "${curl_args[@]}" zx2c4.com/ip
|
||||||
}
|
}
|
||||||
|
|
||||||
main() {
|
main() {
|
||||||
check_deps
|
check_deps
|
||||||
parse_args "$@"
|
parse_args "$@"
|
||||||
show_ip
|
show_ip
|
||||||
}
|
}
|
||||||
|
|
||||||
main "$@"
|
main "$@"
|
||||||
|
|||||||
Reference in New Issue
Block a user