|
| 1 | +#!/bin/bash |
| 2 | +# ebusctl - tool for accessing ebusd via TCP. |
| 3 | +# this can either pass excess arguments to ebusd and then quit, |
| 4 | +# or run interactively if no excess arguments were given. |
| 5 | + |
| 6 | +# print help |
| 7 | +help() { |
| 8 | + cat << EOF |
| 9 | +Usage: ebusctl [OPTION...] [COMMAND...] |
| 10 | +Client for accessing ebusd via TCP. |
| 11 | +
|
| 12 | + Options: |
| 13 | + -s, --server=HOST Connect to ebusd on HOST (name or IP) [localhost] |
| 14 | + -p, --port=PORT Connect to ebusd on PORT [8888] |
| 15 | + -t, --timeout=SECS Timeout for connecting to/receiving from ebusd, 0 for |
| 16 | + none [60] |
| 17 | + -e, --error Exit non-zero if the connection was fine but the response |
| 18 | + indicates non-success |
| 19 | + [COMMAND...] COMMAND (and arguments) to send to ebusd. |
| 20 | +
|
| 21 | +If given, send COMMAND together with arguments to ebusd. |
| 22 | +Use 'help' as COMMAND for help on available ebusd commands. |
| 23 | +EOF |
| 24 | +} |
| 25 | + |
| 26 | +# parse cmdline options |
| 27 | +OPTS=$(getopt -n ebusctl -o 's:p:t:eh' -l 'server:,port:,timeout:,error,help' -- "$@") |
| 28 | +if [ $? -ne 0 ]; then |
| 29 | + help |
| 30 | + exit 1 |
| 31 | +fi |
| 32 | +eval set -- "$OPTS" |
| 33 | +server=localhost |
| 34 | +port=8888 |
| 35 | +timeout=60 |
| 36 | +error= |
| 37 | +while true; do |
| 38 | + case "$1" in |
| 39 | + '-s'|'--server') |
| 40 | + shift |
| 41 | + server="$1" |
| 42 | + shift |
| 43 | + if [ -z "$server" ]; then |
| 44 | + echo "invalid server" >&2 |
| 45 | + exit 1 |
| 46 | + fi |
| 47 | + continue |
| 48 | + ;; |
| 49 | + '-p'|'--port') |
| 50 | + shift |
| 51 | + port="$1" |
| 52 | + shift |
| 53 | + if [ "$port" -lt 1 ] || [ "$port" -gt 65535 ]; then |
| 54 | + echo "invalid port" >&2 |
| 55 | + exit 1 |
| 56 | + fi |
| 57 | + continue |
| 58 | + ;; |
| 59 | + '-t'|'--timeout') |
| 60 | + shift |
| 61 | + timeout="$1" |
| 62 | + shift |
| 63 | + if [ "$timeout" -lt 0 ] || [ "$timeout" -gt 3600 ]; then |
| 64 | + echo "invalid timeout" >&2 |
| 65 | + exit 1 |
| 66 | + fi |
| 67 | + continue |
| 68 | + ;; |
| 69 | + '-e'|'--error') |
| 70 | + shift |
| 71 | + error=1 |
| 72 | + continue |
| 73 | + ;; |
| 74 | + '-h'|'--help') |
| 75 | + shift |
| 76 | + help |
| 77 | + exit |
| 78 | + ;; |
| 79 | + '--') |
| 80 | + shift |
| 81 | + break |
| 82 | + ;; |
| 83 | + *) |
| 84 | + echo "error" >&2 |
| 85 | + exit 1 |
| 86 | + ;; |
| 87 | + esac |
| 88 | +done |
| 89 | + |
| 90 | +# set arguments to netcat |
| 91 | +args=(-q 0) |
| 92 | +if [ "$timeout" -gt 0 ]; then |
| 93 | + args+=(-w "$timeout") |
| 94 | +fi |
| 95 | +args+=("$server" "$port") |
| 96 | + |
| 97 | +# check interactive mode |
| 98 | +if [ $# -ne 0 ]; then |
| 99 | + # non-interactive mode |
| 100 | + ret=0 |
| 101 | + while IFS= read -r line; do |
| 102 | + echo "$line" |
| 103 | + if [ -n "$error" ] && [ "${line##ERR:}" != "$line" ]; then |
| 104 | + ret=1 |
| 105 | + fi |
| 106 | + done < <(echo "$@"|nc "${args[@]}") |
| 107 | + exit $ret |
| 108 | +fi |
| 109 | + |
| 110 | +# interactive mode |
| 111 | +echo "entering interactive mode" >&2 |
| 112 | +nc "${args[@]}" |
0 commit comments