#!/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}" == "null" ]] then echo "Could not login. Please verify the host and username/password." exit 13 fi echo "${token}" > token.json setowls } 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 } setowls() { if [ -z ${OWGW_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 == "owls" ) | .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 OWLS=${url} echo "Using ${OWLS}..." else echo "OWLS endpoint is not found:" jq < ${result_file} exit 1 fi else export OWLS=${OWLS_OVERRIDE} fi } logout() { curl ${FLAGS} -X DELETE -H "Content-Type: application/json" \ -H "Authorization: Bearer ${token}" \ "https://${OWSEC}/api/v1/oauth2/${token}" rm -rf token.json } examplesim() { cat > example-sim.json << EOF { "name":"loadsim", "deviceType":"edgecore_ecw5410", "devices":5, "gateway":"https://ucentral.arilia.com:15002", "macPrefix":"030000", "simulationLength":600 } EOF } testargcount() { if [ "$1" != "$2" ]; then echo "Expected $1 arguments but received $2" exit 1 fi } createsim() { if [[ -z "$1" ]]; then echo "Simulation file is not passed as an argument. Please pass the simulation. To get example simulation definition, run 'examplesim' command and check 'example-sim.json' file" exit 1 else curl ${FLAGS} -X POST "https://${OWLS}/api/v1/simulation/0" \ -H "Content-Type: application/json" \ -H "Authorization: Bearer ${token}" \ -H "accept: application/json" \ -d @$1 > ${result_file} jq < ${result_file} fi } listsims() { testargcount 0 $# curl ${FLAGS} -X GET "https://${OWLS}/api/v1/simulation/*" \ -H "Content-Type: application/json" \ -H "Authorization: Bearer ${token}" > ${result_file} jq < ${result_file} } getsim() { testargcount 1 $# curl ${FLAGS} -X GET "https://${OWLS}/api/v1/simulation/$1" \ -H "Content-Type: application/json" \ -H "Authorization: Bearer ${token}" > ${result_file} jq < ${result_file} } deletesim() { testargcount 1 $# curl ${FLAGS} -X DELETE "https://${OWLS}/api/v1/simulation?id=$1" \ -H "Content-Type: application/json" \ -H "Authorization: Bearer ${token}" > ${result_file} jq < ${result_file} } startsim() { testargcount 1 $# curl ${FLAGS} -X POST "https://${OWLS}/api/v1/operation/$1?operation=start" \ -H "Content-Type: application/json" \ -H "Authorization: Bearer ${token}" > ${result_file} jq < ${result_file} } stopsim() { testargcount 2 $# curl ${FLAGS} -X POST "https://${OWLS}/api/v1/operation/$1?operation=stop&runningId=$2" \ -H "Content-Type: application/json" \ -H "Authorization: Bearer ${token}" > ${result_file} jq < ${result_file} } cancelsim() { testargcount 2 $# curl ${FLAGS} -X POST "https://${OWLS}/api/v1/operation/$1?operation=cancel" \ -H "Content-Type: application/json" \ -H "Authorization: Bearer ${token}" > ${result_file} jq < ${result_file} } getsimstats() { testargcount 1 $# curl ${FLAGS} -X GET "https://${OWLS}/api/v1/status/$1" \ -H "Content-Type: application/json" \ -H "Authorization: Bearer ${token}" > ${result_file} jq < ${result_file} } liststatuses() { testargcount 0 $# curl ${FLAGS} -X GET "https://${OWLS}/api/v1/status/*" \ -H "Content-Type: application/json" \ -H "Authorization: Bearer ${token}" > ${result_file} jq < ${result_file} } results() { testargcount 1 $# curl ${FLAGS} -X GET "https://${OWLS}/api/v1/results/$1" \ -H "Content-Type: application/json" \ -H "Authorization: Bearer ${token}" > ${result_file} jq < ${result_file} } help() { echo "Usage " echo " ./cli [" echo " Please look at examplesim for a simulation example" echo " examplesim" echo " Print a sample simulation configuration" echo " listsims" echo " Return all the defined simulations" echo " getsim " echo " Return the details of a simulation definition" echo " deletesim " echo " Delete a simulation definition" echo " startsim " echo " Start running a simulation" echo " stopsim " echo " Stop running a simulation" echo " cancelsim " echo " Cancel running a simulation" echo " getsimstats " echo " Get stats on a running simulation" echo " results " echo " Get stats on a running simulation" } shopt -s nocasematch case "$1" in "help") help ;; "login") login; echo "You successfully logged in..." ; logout ;; "examplesim") examplesim;; "createsim") login; createsim "$2"; logout;; "listsims") login; listsims; logout;; "liststatuses") login; liststatuses; logout;; "getsim") login; getsim "$2"; logout;; "deletesim") login; deletesim "$2"; logout;; "startsim") login; startsim "$2"; logout;; "stopsim") login; stopsim "$2" "$3"; logout;; "cancelsim") login; cancelsim "$2" "$3"; logout;; "getsimstats") login; getsimstats "$2"; logout;; "results") login; results "$2"; logout;; *) help ;; esac