Files
wlan-cloud-ucentralsec/test_scripts/curl/cli
2021-07-02 10:34:02 -07:00

612 lines
23 KiB
Bash
Executable File

#!/bin/bash
#
# License type: BSD 3-Clause License
# License copy: https://github.com/Telecominfraproject/wlan-cloud-UCENTRALSEC/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 [[ "${UCENTRALSEC}" == "" ]]
then
echo "You must set the variable UCENTRALSEC in order to use this script. Something like"
echo "UCENTRALSEC=myauthgateway.isp.com:16001"
exit 1
fi
if [[ "${FLAGS}" == "" ]]
then
FLAGS="-s"
fi
token=""
result_file=result.json
username="tip@ucentral.com"
password="openwifi"
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://${UCENTRALSEC}/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
}
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
}
logout() {
curl ${FLAGS} -X DELETE -H "Content-Type: application/json" \
-H "Authorization: Bearer ${token}" \
"https://${UCENTRALSEC}/api/v1/oauth2/${token}"
rm -rf token.json
}
listendpoints() {
curl ${FLAGS} -X GET "https://${UCENTRALSEC}/api/v1/systemEndpoints" \
-H "accept: application/json" \
-H "Authorization: Bearer ${token}" > ${result_file}
jq < ${result_file}
for index in {0..10}
do
endpointlocation=".endpoints[${index}].uri"
endpointlocationtype=".endpoints[${index}].type"
rawurl="$(cat ${result_file} | jq -r ${endpointlocation})"
svctype="$(cat ${result_file} | jq -r ${endpointlocationtype})"
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-)"
if [[ ${url} != "null" ]]
then
if [[ ${svctype} == "ucentralgw" ]]
then
echo "url: $url"
echo " proto: $proto"
echo " user: $user"
echo " host: $host"
echo " port: $port"
echo " path: $path"
UCENTRALGW=$host
break
fi
fi
done
}
listcommands() {
curl ${FLAGS} -X GET "https://${UCENTRALSEC}/api/v1/commands?serialNumber=$1&limit=3000" \
-H "accept: application/json" \
-H "Authorization: Bearer ${token}" > ${result_file}
jq < ${result_file}
}
newestcommands() {
curl ${FLAGS} -X GET "https://${UCENTRALSEC}/api/v1/commands?serialNumber=$1&newest=true&limit=50" \
-H "accept: application/json" \
-H "Authorization: Bearer ${token}" > ${result_file}
jq < ${result_file}
}
deletecommands() {
curl ${FLAGS} -X DELETE "https://${UCENTRALSEC}/api/v1/commands?serialNumber=$1" \
-H "accept: application/json" \
-H "Authorization: Bearer ${token}"
}
getcapabilities() {
curl -${FLAGS} -X GET "https://${UCENTRALSEC}/api/v1/device/$1/capabilities" \
-H "accept: application/json" \
-H "Authorization: Bearer ${token}" > ${result_file}
jq < ${result_file}
}
deletecapabilities() {
curl ${FLAGS} -X DELETE "https://${UCENTRALSEC}/api/v1/device/$1/capabilities" \
-H "accept: application/json" \
-H "Authorization: Bearer ${token}"
}
listdevices() {
curl ${FLAGS} -X GET "https://${UCENTRALSEC}/api/v1/devices" \
-H "accept: application/json" \
-H "Authorization: Bearer ${token}" > ${result_file}
jq < ${result_file}
}
deletedevice() {
curl ${FLAGS} -X DELETE "https://${UCENTRALSEC}/api/v1/device/$1" \
-H "accept: application/json" \
-H "Authorization: Bearer ${token}"
}
createdevice() {
payload="{ \"serialNumber\": \"$1\", \"UUID\": 1234456, \"configuration\" : $(cat "$2") , \"deviceType\": \"AP_Default\",\"location\": \"\", \"macAddress\": \"$3\", \"manufacturer\": \"Linksys EA8300 (Dallas)\", \"notes\": \"auto created device.\", \"owner\": \"\" }"
curl ${FLAGS} -X POST "https://${UCENTRALSEC}/api/v1/device/$1" \
-H "accept: application/json" \
-H "Authorization: Bearer ${token}" \
-d "$payload" > ${result_file}
jq < ${result_file}
}
reboot() {
payload="{ \"serialNumber\" : \"$1\" }"
curl ${FLAGS} -X POST "https://${UCENTRALSEC}/api/v1/device/$1/reboot" \
-H "accept: application/json" \
-H "Authorization: Bearer ${token}" \
-d "$payload" > ${result_file}
jq < ${result_file}
}
setloglevel() {
payload="{ \"command\" : \"setloglevel\" , \"parameters\" : [ { \"tag\" : \"$1\" , \"value\" : \"$2\" } ] }"
curl ${FLAGS} -X POST "https://${UCENTRALSEC}/api/v1/system" \
-H "accept: application/json" \
-H "Authorization: Bearer ${token}" \
-d "$payload"
}
getloglevels() {
payload="{ \"command\" : \"getloglevels\" }"
curl ${FLAGS} -X POST "https://${UCENTRALSEC}/api/v1/system" \
-H "accept: application/json" \
-H "Authorization: Bearer ${token}" \
-d "$payload"
}
getloglevelnames() {
payload="{ \"command\" : \"getloglevelnames\" }"
curl ${FLAGS} -X POST "https://${UCENTRALSEC}/api/v1/system" \
-H "accept: application/json" \
-H "Authorization: Bearer ${token}" \
-d "$payload"
}
getsubsystemnames() {
payload="{ \"command\" : \"getsubsystemnames\" }"
curl ${FLAGS} -X POST "https://${UCENTRALSEC}/api/v1/system" \
-H "accept: application/json" \
-H "Authorization: Bearer ${token}" \
-d "$payload"
}
getdevicestatus() {
curl ${FLAGS} -X GET "https://${UCENTRALSEC}/api/v1/device/$1/status" \
-H "accept: application/json" \
-H "Authorization: Bearer ${token}" > ${result_file}
jq < ${result_file}
}
upgrade() {
payload="{ \"serialNumber\" : \"$1\" , \"uri\" : \"$2\" }"
curl ${FLAGS} -X POST "https://${UCENTRALSEC}/api/v1/device/$1/upgrade" \
-H "accept: application/json" \
-H "Authorization: Bearer ${token}" \
-d "$payload" > ${result_file}
jq < ${result_file}
}
leds() {
payload="{ \"serialNumber\" : \"$1\" , \"duration\" : $3, \"pattern\" : \"$2\" }"
curl ${FLAGS} -X POST "https://${UCENTRALSEC}/api/v1/device/$1/leds" \
-H "accept: application/json" \
-H "Authorization: Bearer ${token}" \
-d "$payload" > ${result_file}
jq < ${result_file}
}
factory() {
payload="{ \"serialNumber\" : \"$1\" , \"keepRedirector\" : $2 }"
curl ${FLAGS} -X POST "https://${UCENTRALSEC}/api/v1/device/$1/factory" \
-H "accept: application/json" \
-H "Authorization: Bearer ${token}" \
-d "$payload" > ${result_file}
jq < ${result_file}
}
configure() {
payload="{ \"serialNumber\" : \"$1\", \"UUID\" : 123456 , \"configuration\" : $(cat "$2") }"
curl ${FLAGS} -X POST "https://${UCENTRALSEC}/api/v1/device/$1/configure" \
-H "accept: application/json" \
-H "Authorization: Bearer ${token}" \
-d "$payload" > ${result_file}
jq < ${result_file}
}
request() {
payload="{ \"serialNumber\" : \"$1\" , \"message\" : \"$2\" }"
curl ${FLAGS} -X POST "https://${UCENTRALSEC}/api/v1/device/$1/request" \
-H "accept: application/json" \
-H "Authorization: Bearer ${token}" \
-d "$payload" > ${result_file}
jq < ${result_file}
}
wifiscan() {
payload="{ \"serialNumber\" : \"$1\" , \"verbose\" : $2 }"
curl ${FLAGS} -X POST "https://${UCENTRALSEC}/api/v1/device/$1/wifiscan" \
--max-time 20 \
-H "accept: application/json" \
-H "Authorization: Bearer ${token}" \
-d "$payload" > ${result_file}
jq < ${result_file}
}
activescan() {
payload="{ \"serialNumber\" : \"$1\" , \"verbose\" : $2, \"activeScan\" : true}"
curl ${FLAGS} -X POST "https://${UCENTRALSEC}/api/v1/device/$1/wifiscan" \
--max-time 20 \
-H "accept: application/json" \
-H "Authorization: Bearer ${token}" \
-d "$payload" > ${result_file}
jq < ${result_file}
}
trace() {
payload="{ \"serialNumber\" : \"$1\" , \"duration\" : $2, \"network\" : \"$3\" }"
curl ${FLAGS} -X POST "https://${UCENTRALSEC}/api/v1/device/$1/trace" \
-H "accept: application/json" \
-H "Authorization: Bearer ${token}" \
-d "$payload" > ${result_file}
jq < ${result_file}
}
getstats() {
curl ${FLAGS} -X GET "https://${UCENTRALSEC}/api/v1/device/$1/statistics" \
-H "accept: application/json" \
-H "Authorization: Bearer ${token}" > ${result_file}
jq < ${result_file}
}
getlogs() {
curl ${FLAGS} -X GET "https://${UCENTRALSEC}/api/v1/device/$1/logs" \
-H "accept: application/json" \
-H "Authorization: Bearer ${token}" > ${result_file}
jq < ${result_file}
}
deletelogs() {
curl ${FLAGS} -X DELETE "https://${UCENTRALSEC}/api/v1/device/$1/logs" \
-H "accept: application/json" \
-H "Authorization: Bearer ${token}"
}
gethealthchecks() {
curl ${FLAGS} -X GET "https://${UCENTRALSEC}/api/v1/device/$1/healthchecks" \
-H "accept: application/json" \
-H "Authorization: Bearer ${token}" > ${result_file}
jq < ${result_file}
}
createdefaultconfig() {
payload="{ \"name\": \"$1\", \"modelIds\" : \"$2\", \"configuration\" : $(cat "$3")}"
curl ${FLAGS} -X POST "https://${UCENTRALSEC}/api/v1/default_configuration/$1" \
-H "accept: application/json" \
-H "Authorization: Bearer ${token}" \
-d "$payload" > ${result_file}
jq < ${result_file}
}
listdefaultconfigs() {
curl ${FLAGS} -X GET "https://${UCENTRALSEC}/api/v1/default_configurations" \
-H "accept: application/json" \
-H "Authorization: Bearer ${token}" > ${result_file}
jq < ${result_file}
}
addblacklistdevice() {
payload="{ \"devices\" : [ {\"serialNumber\": \"$1\" , \"reason\" : \"$2\" } ] }"
curl ${FLAGS} -X POST "https://${UCENTRALSEC}/api/v1/blacklist" \
-H "accept: application/json" \
-H "Authorization: Bearer ${token}" \
-d "$payload"
}
deleteblacklistdevice() {
curl ${FLAGS} -X DELETE "https://${UCENTRALSEC}/api/v1/blacklist?serialNumber=$1" \
-H "accept: application/json" \
-H "Authorization: Bearer ${token}"
}
getblacklist() {
curl ${FLAGS} -X GET "https://${UCENTRALSEC}/api/v1/blacklist" \
-H "accept: application/json" \
-H "Authorization: Bearer ${token}" > ${result_file}
jq < ${result_file}
}
eventqueue() {
payload="{ \"serialNumber\" : \"$1\" , \"types\" : [ \"dhcp\", \"rrm\" ] }"
curl ${FLAGS} -X POST "https://${UCENTRALSEC}/api/v1/device/$1/eventqueue" \
--max-time 20 \
-H "accept: application/json" \
-H "Authorization: Bearer ${token}" \
-d "$payload" > ${result_file}
jq < ${result_file}
}
selectdevices() {
curl ${FLAGS} -X GET "https://${UCENTRALSEC}/api/v1/devices?select=$1" \
-H "accept: application/json" \
-H "Authorization: Bearer ${token}" > ${result_file}
jq < ${result_file}
}
deviceserialnumbers() {
curl ${FLAGS} -X GET "https://${UCENTRALSEC}/api/v1/devices?serialOnly=true" \
-H "accept: application/json" \
-H "Authorization: Bearer ${token}" > ${result_file}
jq < ${result_file}
}
deviceswithstatus() {
curl ${FLAGS} -X GET "https://${UCENTRALSEC}/api/v1/devices?deviceWithStatus=true" \
-H "accept: application/json" \
-H "Authorization: Bearer ${token}" > ${result_file}
jq < ${result_file}
}
devicecount() {
curl ${FLAGS} -X GET "https://${UCENTRALSEC}/api/v1/devices?countOnly=true" \
-H "accept: application/json" \
-H "Authorization: Bearer ${token}" > ${result_file}
jq < ${result_file}
}
getfile() {
curl ${FLAGS} -X GET "https://${UCENTRALSEC}/api/v1/file/$2?serialNumber=$1" \
-H "accept: application/octet-stream" \
-H "Authorization: Bearer ${token}" \
-o "$2.pcap"
}
rtty() {
curl ${FLAGS} -X GET "https://${UCENTRALSEC}/api/v1/device/$1/rtty" \
-H "accept: application/json" \
-H "Authorization: Bearer ${token}" > ${result_file}
jq < ${result_file}
cid=$(cat ${result_file} | jq -r '.connectionId')
vport=$(cat ${result_file} | jq -r '.viewport')
server=$(cat ${result_file} | jq -r '.server')
url=https://${server}:${vport}/connect/${cid}
findbrowser
if [[ "${browser}" != "" ]]
then
${browser} ${url}
fi
}
lifetimestats() {
curl ${FLAGS} -X GET "https://${UCENTRALSEC}/api/v1/device/$1/statistics?lifetime=true" \
-H "accept: application/json" \
-H "Authorization: Bearer ${token}" > ${result_file}
jq < ${result_file}
}
laststats() {
curl ${FLAGS} -X GET "https://${UCENTRALSEC}/api/v1/device/$1/statistics?lastOnly=true" \
-H "accept: application/json" \
-H "Authorization: Bearer ${token}" > ${result_file}
jq < ${result_file}
}
lasthealthcheck() {
curl ${FLAGS} -X GET "https://${UCENTRALSEC}/api/v1/device/$1/healthchecks?lastOnly=true" \
-H "accept: application/json" \
-H "Authorization: Bearer ${token}" > ${result_file}
jq < ${result_file}
}
neweststats() {
curl ${FLAGS} -X GET "https://${UCENTRALSEC}/api/v1/device/$1/statistics?newest=true&limit=50" \
-H "accept: application/json" \
-H "Authorization: Bearer ${token}" > ${result_file}
jq < ${result_file}
}
newestlogs() {
curl ${FLAGS} -X GET "https://${UCENTRALSEC}/api/v1/device/$1/logs?newest=true&limit=50" \
-H "accept: application/json" \
-H "Authorization: Bearer ${token}" > ${result_file}
jq < ${result_file}
}
newesthealthchecks() {
curl ${FLAGS} -X GET "https://${UCENTRALSEC}/api/v1/device/$1/healthchecks?newest=true&limit=50" \
-H "accept: application/json" \
-H "Authorization: Bearer ${token}" > ${result_file}
jq < ${result_file}
}
ouilookup() {
curl ${FLAGS} -X GET "https://${UCENTRALSEC}/api/v1/ouis?macList=$1" \
-H "accept: application/json" \
-H "Authorization: Bearer ${token}" > ${result_file}
jq < ${result_file}
}
help() {
echo
echo "listendpoints Get all the system endpoints."
echo "deletedevice <serial> Delete the device."
echo "createdevice <serial> <cfg> <MAC> Create a device using the default configuration."
echo " cfg: JSON config file name"
echo " <MAC>: string MAC Address"
echo "getdevicestatus <serial> Get the device status JSON document."
echo "getstats <serial> Get statistics for the device."
echo "gethealthchecks <serial> Get healthchecks for a device."
echo "newesthealthchecks <serial> Get newest healthchecks for a device."
echo "lasthealthcheck <serial> Get the last healthcheck"
echo
echo "getcapababilities <serial> Get the device capabilities JSON document."
echo "deletecapababilities <serial> Delete the device capabilities JSON."
echo
echo "reboot <serial> Reboot the device."
echo "upgrade <serial> <URI> Do firmware upgrade for a device."
echo "leds <serial> <pattern> <d> Activate LEDs a device."
echo " pattern: on/off/blink"
echo " d: number in seconds"
echo "configure <serial> <cfg> Change configuration for a device."
echo " cfg must be valid JSON config."
echo "factory <serial> <true/false> Do factory reset for device. true=keep redirector, false=reset redirector"
echo "request <serial> <message> Force a message from the device."
echo " message=state/healthcheck"
echo "wifiscan <serial> <verbose> Do wifiscan for a device."
echo " verbose=true/false"
echo "active <serial> <verbose> Do an active wifiscan for a device."
echo " verbose=true/false"
echo "trace <serial> <d> <n> Launch a remote trace for a device."
echo " d=number of seconds"
echo " n=lan/wan"
echo
echo "getcommand <command-uuid> Get the command JSON document."
echo "deletecommand <command-uuid> Delete the command."
echo "newestcommands <serial> Get the newest commands for a device."
echo "listdevices List devices"
echo "listcommands <serial> List commands for a specific device"
echo "deletecommands <serial> Delete commands for a device."
echo
echo "getlogs <serial> Get logs for the device."
echo "newestlogs <serial> Get the latest logs for the device."
echo "deletelogs <serial> Delete logs for the device."
echo "eventqueue <serial> Request event queue for the device."
echo
echo "listdefaultconfigs List a default configurations"
echo "createdefaultconfig <name> <ids> <cfg> Create a default configuration"
echo " name=unique name, no spaces"
echo " ids=coma separated list of models"
echo " cfg=config file name"
echo
echo "addblacklistdevice <serial> <r> Add a device to the black list"
echo " <r> Reason for blacklisting"
echo "getblacklist List all blacklisted devices"
echo "deleteblacklistdevice <serial> Add a device to the black list"
echo
echo "devicecount Get the number of devices in the DB"
echo "deviceserialnumbers Get only the serial numbers"
echo "selectdevices <serial list> get a list of serial numbers (must be comma separated)"
echo "deviceswithstatus Get devices with their status"
echo
echo "setloglevel <sys> <level> Set the logging system level for individual subsystems."
echo " sys:ufileuploader/websocket/storage/restapi/commandmanager/auth/deviceregistry/all"
echo " level:none/fatal/critical/error/warning/notice/information/debug/trace"
echo "getloglevels Get the log levels of all the subsystems"
echo "getloglevelnames Get the list of log level names possible"
echo "getsubsystemnames Get the subsystems that can be used when setting log levels."
echo "getfile <serial> <uuid> <name> Get the file associated with trace command <uuid> for device <serial>"
echo " The file will be saved with the name <name>"
echo
echo "rtty <serial> Get the details for an rtty session."
echo
echo "lifetimestats <serial> Get the lifetime stats counters for a device"
echo "laststats <serial> Get the last statistics for a device"
echo "neweststats <serial> Get the newest statistics for a device"
echo
echo "ouilookup <serial> Lookup an OUI"
echo
echo
echo "To pass additional flags to the CURL command, create an environment variable called FLAGS and git ve the values you"
echo "want. For example, for force all call to use IPv6, set FLAGS=\"-6\", for verbose mode and IPv6, set FLAGS=\"-6 -v\""
echo
echo
}
shopt -s nocasematch
case "$1" in
"listendpoints") login; listendpoints ; logout ;;
"help") login; help ; logout ;;
"getcommand") login; getcommand "$2" ; logout ;;
"deletecommand") login; deletecommand "$2" ; logout ;;
"getcapabilities") login; getcapabilities "$2" ; logout ;;
"deletecapabilities") login; deletecapabilities "$2" ; logout ;;
"listdevices") login; listdevices ; logout ;;
"deletedevice") login; deletedevice "$2" ; logout ;;
"createdevice") login; createdevice "$2" "$3" ; logout ;;
"reboot") login; reboot "$2" ; logout ;;
"setloglevel") login; setloglevel "$2" "$3" ; logout ;;
"getdevicestatus") login; getdevicestatus "$2" ; logout ;;
"upgrade") login; upgrade "$2" "$3" ; logout ;;
"factory") login; factory "$2" "$3" ; logout ;;
"leds") login; leds "$2" "$3" "$4" ; logout ;;
"listcommands") login; listcommands "$2" ; logout ;;
"deletecommands") login; deletecommands "$2" ; logout ;;
"configure") login; configure "$2" "$3" ; logout ;;
"request") login; request "$2" "$3" ; logout ;;
"wifiscan") login; wifiscan "$2" "$3" ; logout ;;
"activescan") login; activescan "$2" "$3" ; logout ;;
"trace") login; trace "$2" "$3" "$4" ; logout ;;
"getstats") login; getstats "$2" ; logout ;;
"getlogs") login; getlogs "$2" ; logout ;;
"deletelogs") login; deletelogs "$2" ; logout ;;
"gethealthchecks") login; gethealthchecks "$2" ; logout ;;
"createdefaultconfig") login; createdefaultconfig "$2" "$3" "$4" ; logout ;;
"listdefaultconfigs") login; listdefaultconfigs "$2" ; logout ;;
"addblacklistdevice") login; addblacklistdevice "$2" "$3" ; logout ;;
"deleteblacklistdevice") login; deleteblacklistdevice "$2" ; logout ;;
"getblacklist") login; getblacklist ; logout ;;
"eventqueue") login; eventqueue "$2" ; logout ;;
"selectdevices") login; selectdevices "$2" ; logout ;;
"deviceserialnumbers") login; deviceserialnumbers ; logout ;;
"devicecount") login; devicecount ; logout ;;
"deviceswithstatus") login; deviceswithstatus ; logout ;;
"getfile") login; getfile "$2" "$3" ; logout ;;
"rtty") login; rtty "$2" ; logout ;;
"lifetimestats") login; lifetimestats "$2"; logout;;
"laststats") login; laststats "$2"; logout;;
"newestcommands") login; newestcommands "$2"; logout;;
"neweststats") login; neweststats "$2"; logout;;
"newestlogs") login; newestlogs "$2"; logout;;
"newesthealthchecks") login; newesthealthchecks "$2"; logout;;
"lasthealthcheck") login; lasthealthcheck "$2"; logout;;
"login") login ;;
"findbrowser") findbrowser; echo "Browser: ${browser}" ;;
"getloglevels") login; getloglevels; logout ;;
"getloglevelnames") login; getloglevelnames; logout ;;
"getsubsystemnames") login; getsubsystemnames; logout ;;
"ouilookup") login; ouilookup "$2"; logout;;
*) help ;;
esac