-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfastd-query
executable file
·79 lines (69 loc) · 1.57 KB
/
fastd-query
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/bin/bash
FASTD_SOCKET=${FASTD_SOCKET:-}
SOCAT=$(which socat 2>/dev/null)
JQ=$(which jq 2>/dev/null)
while getopts s: option
do
case "${option}"
in
s) FASTD_SOCKET=${OPTARG};;
esac
done
echo "FastD Socket: $FASTD_SOCKET"
socketpipe() {
"$SOCAT" - UNIX:"$FASTD_SOCKET"
}
help_general(){
echo "Usage: $0 OBJECT { COMMAND | help } -s SOCKET_PATH"
echo "where OBJECT := { STATISTICS | connections | peers }"
echo " STATISTICS := statistics JQ-STRING"
}
if [ "x${SOCAT}" = "x" ]; then
echo "Error: 'socat' not found in PATH"
exit 2
fi
if [ "x${JQ}" = "x" ]; then
echo "Error: 'jq' not found in PATH"
exit 2
fi
if [ "x${FASTD_SOCKET}" = "x" ]; then
echo "Error: No fastd socket declarded"
help_general
exit 1
fi
if [ ! -S "${FASTD_SOCKET}" ]; then
echo "Error: File '${FASTD_SOCKET}' does not exists, or is no socket"
fi
case $3 in
peers|p)
shift
QUERY=".peers[]"
case $1 in
name|n)
QUERY="${QUERY} | select(.name == \"${2}\")"
shift 2
;;
mac|m)
QUERY="${QUERY} | select( .connection | .mac_addresses[]? == \"${2}\")"
shift 2
;;
help|h)
echo "Usage: $0 peers name STRING JQ-STRING"
echo " $0 peers mac LLADDR JQ-STRING"
echo " $0 peers JQ-STRING"
exit 0
;;
esac
socketpipe | "$JQ" "$QUERY | ${@:-.}"
;;
connections|c)
socketpipe | "$JQ" '.peers[] | select( .connection ) | .name' | wc -l
;;
statistics|s)
shift
socketpipe | "$JQ" ".statistics[] | ${@:-.}"
;;
*|help)
help_general
;;
esac