-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from amigus/client
Add a simple CLI based on POSIX shell, cURL and jq.
- Loading branch information
Showing
10 changed files
with
342 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# Dnsmasq Web UNIX/Linux shell client | ||
|
||
A composable collection of POSIX shell scripts that constitute a client of Dnsmasq Web. | ||
|
||
## use.sh | ||
|
||
Contains the `dnsmasq_web_use` command. | ||
It checks whether the argument(s) are available as a shell command. | ||
It prevents the definition of functions that have unmet prerequisites. | ||
It must be "dotted" into the shell first. | ||
It will set DNSMASQ_WEB_SERVER to `[::1]`, i.e., _localhost_, and, | ||
DNSMASQ_WEB_SERVER_USER to _root_ unless they are already set. | ||
|
||
```sh | ||
. /path/to/cli/use.sh | ||
``` | ||
|
||
## token.sh | ||
|
||
`dnsmasq_web_token` gets a token from the UNIX domain socket. | ||
When `$DNSMASQ_WEB_SERVER` is set to something other than the default, | ||
"[::1]", i.e., the server is remote, | ||
it will use `ssh -ntq` to run the command on `$DNSMASQ_WEB_SERVER`. | ||
Consider configuring SSH to connect to it without prompting for a password/passphrase. | ||
|
||
## curl.sh | ||
|
||
`dnsmasq_web_curl` uses cURL to access Dnsmasq Web. | ||
It does two things: | ||
|
||
1. It uses `$DNSMASQ_WEB_SERVER` instead of requiring the full URL as an argument | ||
1. It uses `$DNSMASQ_WEB_TOKEN` to add the `X-Token` header to each request | ||
|
||
There are also two commands to manage _reservations_: | ||
|
||
1. dnsmasq_web_reservations_add | ||
1. dnsmasq_web_reservations_delete | ||
|
||
## curl_jq.sh | ||
|
||
`dnsmasq_web_curl_jq` processes the output of `dnsmasq_web_curl` using `jq`. | ||
If has hooks to inject pre-built `jq` logic for formatting, processing and sorting. | ||
|
||
## jq_commands.sh | ||
|
||
Contains some pre-built `jq` arguments for _clients_, _leases_, and _reservations_. | ||
It uses `eval` to define shell functions for: | ||
|
||
1. dnsmasq_web_curl_clients | ||
1. dnsmasq_web_clients | ||
1. dnsmasq_web_curl_leases | ||
1. dnsmasq_web_leases | ||
1. dnsmasq_web_curl_reservations | ||
1. dnsmasq_web_reservations | ||
|
||
The shortened command presents the data as a table. | ||
|
||
## reservations.sh | ||
|
||
Adds commands to manage _reservations_ that also use `jq`: | ||
|
||
1. dnsmasq_web_reservations_add | ||
1. dnsmasq_web_reservations_change | ||
1. dnsmasq_web_reservations_delete |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#!/bin/sh | ||
dnsmasq_web_use curl || { | ||
echo no dnsmasq_web_curl without curl && return | ||
} | ||
|
||
dnsmasq_web_requires_token() { | ||
test -n "$DNSMASQ_WEB_TOKEN" -o -n "$DNSMASQ_WEB_TOKEN_CMD" | ||
} && ! dnsmasq_web_requires_token || dnsmasq_web_use dnsmasq_web_token || { | ||
echo dnsmasq_web_curl requires dnsmasq_web_token && return | ||
} | ||
|
||
dnsmasq_web_try_curl() { | ||
url="http://$DNSMASQ_WEB_SERVER/${1%%/}" | ||
shift | ||
args='--fail-with-body --silent' | ||
dnsmasq_web_requires_token && args="$args --header 'X-Token: $( | ||
if test -n "$DNSMASQ_WEB_TOKEN"; then | ||
echo "$DNSMASQ_WEB_TOKEN" | ||
else | ||
dnsmasq_web_token | ||
fi | ||
)'" | ||
eval curl "$args" "$url" "$*" | ||
} | ||
|
||
dnsmasq_web_curl() { | ||
output=$(dnsmasq_web_try_curl "$@") | ||
test $? -ne 22 && echo "$output" && return | ||
if test "$output" = '{"error":"Unauthorized token"}'; then | ||
printf '%s expired; renewing' "$DNSMASQ_WEB_TOKEN" >&2 | ||
else | ||
echo "$output" && return | ||
fi | ||
DNSMASQ_WEB_TOKEN=$(dnsmasq_web_token) && | ||
dnsmasq_web_try_curl "$@" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#!/bin/sh | ||
|
||
dnsmasq_web_use dnsmasq_web_curl jq || { | ||
echo no dnsmasq_web_curl_jq without jq && | ||
return | ||
} | ||
dnsmasq_web_curl_jq() { | ||
noun="$(echo "$1" | sed -e 's|[/?].*||')" | ||
sort="$(eval echo "\$dnsmasq_web_${noun}_jq_sort")" | ||
process="$(eval echo "\$dnsmasq_web_${noun}_jq")" | ||
expression="." | ||
test -n "$sort" && expression="$sort | .[]" | ||
test -n "$process" && expression="$expression | $process" | ||
dnsmasq_web_curl "$@" | jq "$expression" | ||
} | ||
|
||
dnsmasq_web_use dnsmasq_web_curl column || { | ||
echo using cat in place of column for dnsmasq_web_tabulate >&2 | ||
column() { cat; } | ||
} | ||
dnsmasq_web_tabulate() { | ||
jq -r '@tsv' | column -t | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#!/bin/sh | ||
|
||
# shellcheck disable=SC2034 | ||
dnsmasq_web_clients_jq_sort='sort_by(.requests)' | ||
# shellcheck disable=SC2034 | ||
dnsmasq_web_clients_jq=' | ||
.ipv4s |= (.|join(","))[0:30] | | ||
.vendor_class |= (.|gsub("[\\s]+"; " "))[0:15] | | ||
if .hostname == "" then | ||
.hostname="<empty>" | ||
end | | ||
if .vendor_class == "" then | ||
.vendor_class="<empty>" | ||
end | | ||
if (.ipv4s|length) > 28 then | ||
.ipv4s |= .[0:27] + "..." | ||
end | | ||
if (.vendor_class|length) > 13 then | ||
.vendor_class |= .[0:12] + "..." | ||
end | | ||
[.requests,.mac,.hostname,.ipv4s,.vendor_class]' | ||
dnsmasq_sort_by_ipv4='sort_by(.ipv4|split("\\."; null)[3]|tonumber)' | ||
# shellcheck disable=SC2034 | ||
dnsmasq_web_leases_jq_sort="$dnsmasq_sort_by_ipv4" | ||
# shellcheck disable=SC2034 | ||
dnsmasq_web_leases_jq=' | ||
if .hostname == "" then | ||
.hostname="<empty>" | ||
end | | ||
if .renewed == "" then | ||
.renewed=.added | ||
end | | ||
[.mac,.ipv4,.hostname,.added,.renewed,.age]' | ||
# shellcheck disable=SC2034 | ||
dnsmasq_web_reservations_jq_sort="$dnsmasq_sort_by_ipv4" | ||
# shellcheck disable=SC2034 | ||
dnsmasq_web_reservations_jq=' | ||
[.mac,.ipv4,.hostname,(.tags|join(","))]' | ||
|
||
dnsmasq_web_use dnsmasq_web_curl_jq || return | ||
for noun in clients leases reservations; do | ||
eval "dnsmasq_web_curl_$noun() { dnsmasq_web_curl_jq $noun \"\$@\"; }; | ||
dnsmasq_web_$noun() { dnsmasq_web_curl_$noun | dnsmasq_web_tabulate; }" | ||
done |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#!/bin/sh | ||
|
||
dnsmasq_web_use dnsmasq_web_curl || return | ||
dnsmasq_web_reservation_add() { | ||
{ | ||
echo '{"mac":"'"$1"'","ipv4":"'"$2"'","hostname":"'"$3"'"' | ||
test -n "$4" && echo ',"tags":["'"$4"'"]' | ||
test -n "$5" && echo ',"lease_time":"'"$5"'"' | ||
echo '}' | ||
} | dnsmasq_web_curl reservations -X POST -d @- | ||
} | ||
|
||
dnsmasq_web_reservation_delete() { | ||
dnsmasq_web_curl "reservations/$1" -X DELETE | ||
} | ||
|
||
dnsmasq_web_use jq || return | ||
dnsmasq_web_reservation_change() { | ||
mac="$1" | ||
shift | ||
dnsmasq_web_curl "reservations/$mac" | | ||
jq ".$1 = $2" | | ||
dnsmasq_web_curl "reservations/$mac" -X PUT -d @- | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#!/bin/sh | ||
|
||
myname=$(basename "$0") | ||
if command -v "$myname" >/dev/null 2>&1 && [ "$(type "$myname")" = "function" ]; then | ||
"$myname" "$@" | ||
else | ||
echo "Function '$myname' is not defined." | ||
return 1 | ||
fi |
Oops, something went wrong.