#!/bin/bash # # License type: BSD 3-Clause License # License copy: https://github.com/Telecominfraproject/wlan-cloud-ucentralgw/blob/master/LICENSE # # Created by Stephane Bourque on 2021-03-04. # Arilia Wireless Inc. # if [[ "$(which jq)" == "" ]] then echo "You need the package jq installed to use this script." exit 1 fi if [[ "$(which curl)" == "" ]] then echo "You need the package curl installed to use this script." exit 1 fi if [[ -z "${OWSEC}" || -z "${OWSEC_USERNAME}" || -z "${OWSEC_PASSWORD}" ]] then echo "You must set the variables OWSEC, OWSEC_USERNAME, and OWSEC_PASSWORD in order to use this script. Something like" echo "export OWSEC=security.isp.com:16001" echo "export OWSEC_USERNAME=theusername@domain.com" echo "export OWSEC_PASSWORD=mytoughpassword" exit 1 fi username=${OWSEC_USERNAME} password=${OWSEC_PASSWORD} if [[ "${FLAGS}" == "" ]] then FLAGS="-s" fi token="" result_file=result.json browser_list=(firefox sensible-browser xdg-open w3m links links2 lynx youtube-dl) browser="" login() { payload="{ \"userId\" : \"$username\" , \"password\" : \"$password\" }" token=$(curl ${FLAGS} -X POST -H "Content-Type: application/json" -d "$payload" "https://${OWSEC}/api/v1/oauth2" | jq -r '.access_token') if [[ "${token}" == "" ]] then echo "Could not login. Please verify the host and username/password." exit 13 fi echo "${token}" > token.json setfms } findbrowser() { if [[ "${browser}" != "" ]] then echo elif [[ "$(uname)" == "Darwin" ]] then browser=open else BROWSER_LIST=(firefox sensible-browser xdg-open w3m links links2 lynx youtube-dl) for br_name in "${browser_list[@]}" do if [[ $(which ${br_name}) != "" ]] then browser=${br_name} break fi done fi } setfms() { if [ -z ${OWFMS_OVERRIDE+x} ]; then curl ${FLAGS} -X GET "https://${OWSEC}/api/v1/systemEndpoints" \ -H "accept: Application/json" \ -H "Authorization: Bearer ${token}" > ${result_file} rawurl="$(cat ${result_file} | jq -r '.endpoints[] | select( .type == "owfms" ) | .uri')" if [[ ! -z "${rawurl}" ]]; then proto="$(echo $rawurl | grep :// | sed -e's,^\(.*://\).*,\1,g')" url="$(echo ${rawurl/$proto/})" user="$(echo $url | grep @ | cut -d@ -f1)" hostport="$(echo ${url/$user@/} | cut -d/ -f1)" host="$(echo $hostport | sed -e 's,:.*,,g')" port="$(echo $hostport | sed -e 's,^.*:,:,g' -e 's,.*:\([0-9]*\).*,\1,g' -e 's,[^0-9],,g')" path="$(echo $url | grep / | cut -d/ -f2-)" export OWFMS=${url} echo "Using ${OWFMS}..." else echo "OWFMS endpoint is not found:" jq < ${result_file} exit 1 fi else export OWFMS=${OWFMS_OVERRIDE} fi } logout() { curl ${FLAGS} -X DELETE "https://${OWSEC}/api/v1/oauth2/${token}" \ -H "Content-Type: application/json" \ -H "Authorization: Bearer ${token}" rm -rf token.json } getfirmwares() { curl ${FLAGS} -X GET "https://${OWFMS}/api/v1/firmwares?deviceType=$1" \ -H "Content-Type: application/json" \ -H "Authorization: Bearer ${token}" \ -H "Accept: application/json" > ${result_file} jq < ${result_file} } updatefirmwares() { curl ${FLAGS} -X PUT "https://${OWFMS}/api/v1/firmwares?update=true" \ -H "Content-Type: application/json" \ -H "Authorization: Bearer ${token}" \ -H "Accept: application/json" > ${result_file} jq < ${result_file} } updatefirmwarestime() { curl ${FLAGS} -X GET "https://${OWFMS}/api/v1/firmwares?updateTimeOnly=true" \ -H "Content-Type: application/json" \ -H "Authorization: Bearer ${token}" \ -H "Accept: application/json" > ${result_file} jq < ${result_file} } latestfirmware() { curl ${FLAGS} -X GET "https://${OWFMS}/api/v1/firmwares?latestOnly=true&deviceType=$1" \ -H "Content-Type: application/json" \ -H "Authorization: Bearer ${token}" \ -H "Accept: application/json" > ${result_file} jq < ${result_file} } latestfirmwarerc() { curl ${FLAGS} -X GET "https://${OWFMS}/api/v1/firmwares?latestOnly=true&deviceType=$1&rcOnly=true" \ -H "Content-Type: application/json" \ -H "Authorization: Bearer ${token}" \ -H "Accept: application/json" > ${result_file} jq < ${result_file} } revisions() { curl ${FLAGS} -X GET "https://${OWFMS}/api/v1/firmwares?revisionSet=true" \ -H "Content-Type: application/json" \ -H "Authorization: Bearer ${token}" \ -H "Accept: application/json" > ${result_file} jq < ${result_file} } devicetypes() { curl ${FLAGS} -X GET "https://${OWFMS}/api/v1/firmwares?deviceSet=true" \ -H "Content-Type: application/json" \ -H "Authorization: Bearer ${token}" \ -H "Accept: application/json" > ${result_file} jq < ${result_file} } firmwareage() { curl ${FLAGS} -G -H "Content-Type: application/json" \ -H "Authorization: Bearer ${token}" \ "https://${OWFMS}/api/v1/firmwareAge" --data-urlencode "deviceType=$1" --data-urlencode "revision=$2" > ${result_file} jq < ${result_file} } gethistory() { curl ${FLAGS} -X GET "https://${OWFMS}/api/v1/revisionHistory/$1" \ -H "Content-Type: application/json" \ -H "Authorization: Bearer ${token}" \ -H "Accept: application/json" > ${result_file} jq < ${result_file} } connecteddevice() { curl ${FLAGS} -X GET -H "Content-Type: application/json" \ -H "Authorization: Bearer ${token}" \ "https://${OWFMS}/api/v1/connectedDevice/$1" > ${result_file} jq < ${result_file} } connecteddevices() { curl ${FLAGS} -X GET "https://${OWFMS}/api/v1/connectedDevices" \ -H "Content-Type: application/json" \ -H "Authorization: Bearer ${token}" \ -H "Accept: application/json" > ${result_file} jq < ${result_file} } devicereport() { curl ${FLAGS} -X GET "https://${OWFMS}/api/v1/deviceReport" \ -H "Content-Type: application/json" \ -H "Authorization: Bearer ${token}" \ -H "Accept: application/json" > ${result_file} jq < ${result_file} } setloglevel() { payload="{ \"command\" : \"setloglevel\" , \"subsystems\" : [ { \"tag\" : \"$1\" , \"value\" : \"$2\" } ] }" curl ${FLAGS} -X POST "https://${OWFMS}/api/v1/system" \ -H "Content-Type: application/json" \ -H "Authorization: Bearer ${token}" \ -H "Accept: application/json" \ -d "$payload" } getloglevels() { payload="{ \"command\" : \"getloglevels\" }" curl ${FLAGS} -X POST "https://${OWFMS}/api/v1/system" \ -H "Content-Type: application/json" \ -H "Authorization: Bearer ${token}" \ -H "Accept: application/json" \ -d "$payload" } getloglevelnames() { payload="{ \"command\" : \"getloglevelnames\" }" curl ${FLAGS} -X POST "https://${OWFMS}/api/v1/system" \ -H "Content-Type: application/json" \ -H "Authorization: Bearer ${token}" \ -H "Accept: application/json" \ -d "$payload" } getsubsystemnames() { payload="{ \"command\" : \"getsubsystemnames\" }" curl ${FLAGS} -X POST "https://${OWFMS}/api/v1/system" \ -H "Content-Type: application/json" \ -H "Authorization: Bearer ${token}" \ -H "Accept: application/json" \ -d "$payload" } systeminfo() { curl ${FLAGS} -X GET "https://${OWFMS}/api/v1/system?command=info" \ -H "Content-Type: application/json" \ -H "Authorization: Bearer ${token}" \ -H "Accept: application/json" > ${result_file} jq < ${result_file} } getdeviceinfo() { curl ${FLAGS} -X GET "https://${OWFMS}/api/v1/deviceInformation/$1" \ -H "Content-Type: application/json" \ -H "Authorization: Bearer ${token}" \ -H "Accept: application/json" > ${result_file} jq < ${result_file} } reloadsubsystem() { payload="{ \"command\" : \"reload\", \"subsystems\" : [ \"$1\" ] }" curl ${FLAGS} -X POST "https://${OWFMS}/api/v1/system" \ -H "Content-Type: application/json" \ -H "Authorization: Bearer ${token}" \ -H "Accept: application/json" \ -d "$payload" } check_response() { if [ -s "$1" ]; then ERROR_CODE="$(jq -r '.ErrorCode' < $1)" if [[ -n "$ERROR_CODE" && "$ERROR_CODE" != 'null' ]]; then echo "Error: got HTTP error code $ERROR_CODE, exiting" exit 1 fi else echo "Error: result file not found or empty" exit 1 fi } test_service() { echo echo "----------------------" echo "Get system information" echo "----------------------" systeminfo check_response $result_file echo echo "-----------------" echo "Get device status" echo "-----------------" SECONDS=0 TIMEOUT_SECONDS=900 while (( $SECONDS < $TIMEOUT_SECONDS )); do connecteddevice $1 &>/dev/null DEVICE_STATUS="$(jq -r '.status' < $result_file)" if [ "$DEVICE_STATUS" != 'connected' ]; then echo "Waiting for firmware service to report device status, $SECONDS seconds elapsed" sleep 30 else jq < $result_file break fi done if (( $SECONDS >= $TIMEOUT_SECONDS )); then echo "Error: timed out getting firmware list" exit 1 fi echo echo "-----------------" echo "Get firmware list" echo "-----------------" SECONDS=0 TIMEOUT_SECONDS=900 while (( $SECONDS < $TIMEOUT_SECONDS )); do getfirmwares &>/dev/null check_response $result_file FIRMWARE_STATUS="$(jq -r '.firmwares' < $result_file)" if [ "$FIRMWARE_STATUS" == '[]' ]; then echo "Waiting for service to retrieve firmware list, $SECONDS seconds elapsed" sleep 30 else jq < $result_file break fi done if (( $SECONDS >= $TIMEOUT_SECONDS )); then echo "Error: timed out getting firmware list" exit 1 fi } help() { echo "Usage: cli [args]" echo echo "getfirmwares Get a lit of firmwares" echo "latestfirmware Get the latest firmware for the device_type specified" echo "revisions Get a list of revisions available." echo "devicetypes Get the list of device types supported." echo "firmwareage Calculate how out of date a specific release it." echo "gethistory Get the device firmware history." echo "connecteddevice Get the device status." echo "connectedDevices Get the list of connected devices." echo "devicereport Get the dashboard." echo "setloglevel Set the log level for s specific subsystem." echo "getloglevels Get the current log levels for all subsystems." echo "getloglevelnames Get the log level names available." echo "getsubsystemnames Get the list of subsystems." echo "systeminfo Get basic system information." echo "reloadsubsystem Reload the configuration for a subsystem." echo "test_service Run a set of CLI commands for testing purposes." echo echo } shopt -s nocasematch case "$1" in "login") login; help ; logout ;; "getfirmwares") login; getfirmwares "$2"; logout ;; "updatefirmwares") login; updatefirmwares ; logout ;; "updatefirmwarestime") login; updatefirmwarestime ; logout ;; "latestfirmware") login; latestfirmware "$2"; logout ;; "latestfirmwarerc") login; latestfirmwarerc "$2"; logout ;; "revisions") login; revisions; logout ;; "devicetypes") login; devicetypes; logout ;; "firmwareage") login; firmwareage "$2" "$3"; logout ;; "gethistory") login; gethistory "$2"; logout ;; "connecteddevices") login; connecteddevices ; logout ;; "connecteddevice") login; connecteddevice "$2" ; logout ;; "devicereport") login; devicereport; logout ;; "setloglevel") login; setloglevel "$2" "$3" ; logout ;; "getloglevels") login; getloglevels; logout ;; "getloglevelnames") login; getloglevelnames; logout ;; "getsubsystemnames") login; getsubsystemnames; logout ;; "reloadsubsystem") login; reloadsubsystem "$2"; logout ;; "systeminfo") login; systeminfo ; logout;; "test_service") login; test_service $2; logout;; "getdeviceinfo") login; getdeviceinfo $2; logout;; *) help ;; esac