I have created a great script to update the channel tags. Happy to share with the community
`
#!/bin/bash
type agefile.sh >/dev/null 2>&1 { 2>&1 echo "Missing dependency: This script requires agefile.sh"; exit 1; }
type curl >/dev/null 2>&1 { 2>&1 echo "Missing dependency: This script requires curl"; exit 1; }
type date >/dev/null 2>&1 { 2>&1 echo "Missing dependency: This script requires date"; exit 1; }
type grep >/dev/null 2>&1 { 2>&1 echo "Missing dependency: This script requires grep"; exit 1; }
type jq >/dev/null 2>&1 { 2>&1 echo "Missing dependency: This script requires jq"; exit 1; }
type sed >/dev/null 2>&1 { 2>&1 echo "Missing dependency: This script requires sed"; exit 1; }
type wc >/dev/null 2>&1 || { 2>&1 echo "Missing dependency: This script requires wc"; exit 1; }
#Defaults
host='<DNSNAME>'
port='9981'
uid='<UID>'
pwd='<PWD>'
lookupsortindex='<FIND NUM>'
replacesortindex='<REPLACE WITH NUM>'
comment=$(echo "Updated by API" $(date)|sed -r 's/\ /_/g')
search='.entries[].uuid'
inc_disabled='0'
TMPDIR="/tmp"
LOGDIR='/tmp'
LOGFILENAME='<FILENAME>.log'
LOGFILE=""$LOGDIR"/"$LOGFILENAME""
VERBOSE='0'
TEST='0'
usage() {
cat <<EOF
Usage: [-l -n -p -s -r -t -v -h]
where:
-l Directory to save log file
-n Hostname
-u User ID
-p Password
-s Source Sort Index
-r Replacement Sort Index
-t Test run only (no change)
-v Verbose output
-h Show this usage help
EOF
exit 0
}
function disp() {
#Log to Syslog, logfile and Std out unless -x, -l or -s is used
#-a override and output to all except if -x used
#-l output to logfile only
#-s output to syslog only
#-v output to syslog and display (use before logfile enabled)
#-x output on std out only
TEXT="$1" #display text
OPT="$2" #optional
#[[ ! -n "$OPT" ]] && echo "Opt Null" || echo "Opt Not Null"
#[[ -z "$OPT" ]] && echo "Opt Empty" || echo "Opt Not Empty"
if [[ "$OPT" == "-x" ]] ; then
echo "$(date +"%Y-%m-%d_%T"): $TEXT"
else
#Short notation - if Verbose output to std out
[[ $VERBOSE ]] || [[ "$OPT" == "-a" ]] || [[ "$OPT" == "-v" ]] && echo "$(date +"%Y-%m-%d_%T"): $TEXT"
#output to logfile
if [[ "$OPT" == "-a" ]] || [[ "$OPT" == "-l" ]] ||[[ ! "$OPT" ]] ; then
#only output to log file if the filename is defined and directory exists
if ([[ -d "$LOGDIR" ]] && ([[ -n "$LOGFILENAME" ]] && [[ ! -z "$LOGFILENAME" ]])) ; then
[[ "$OPT" == "-a" ]] || [[ "$OPT" == "-l" ]] || [[ ! "$OPT" ]] && echo "$(date +"%Y-%m-%d_%T"): $TEXT" >> $LOGFILE
fi
fi
#output to system log
[[ "$OPT" == "-a" ]] || [[ "$OPT" == "-s" ]] || [[ "$OPT" == "-v" ]] || [[ ! "$OPT" ]] && logger "$0: $TEXT"
fi
}
Usage: [-d -l -n -p -s -r -v -h]
while getopts "dl:n:p:s:r:tvh" opt; do
case "$opt" in
d)
inc_disabled=1 >&2;
;;
l)
LOGFILENAME=$OPTARG >&2;
;;
n)
host=$OPTARG >&2;
;;
p)
port=$OPTARG >&2;
;;
s)
lookupsortindex=$OPTARG >&2;
;;
r)
replacesortindex=$OPTARG <&2;
;;
t)
TEST=1 <&2;
;;
v)
VERBOSE=1 >&2;
;;
h)
usage
exit 1
;;
๐
>&2 echo
>&2 echo "Option -$OPTARG requires an argument."
>&2 usage
exit 1
;;
\?)
>&2 echo
>&2 echo "Invalid option: -$OPTARG"
>&2 usage
exit 1
;;
esac
done
function input_check() {
#LOG DIR -l
#Check if log directory exists, and if not try to use /tmp
if [ ! -d "$LOGDIR" ] ;
then
disp "$(date +"%Y-%m-%d%T"): Log directory '$LOGDIR' does not exist...attempting to use '$TMPDIR'" -v
LOGDIR="$TMPDIR"
fi
if [ ! -d "$LOGDIR" ] ;
then
disp "$(date +"%Y-%m-%d%T"): Log directory '$LOGDIR' does not exist. Exiting!" -v
exit 1
fi
}
function input_show() {
#Output usage parameters
disp "Host: $host"
disp "Port: $port"
disp "Username: $uid"
disp "Password: $pwd"
disp "Include disabled: $inc_disabled"
disp "Source Serach Index: $lookupsortindex"
disp "Replacement Serach Index: $replacesortindex"
disp "Search JSON Key: $search"
disp "Log Dir: $LOGDIR"
disp "Temp Dir: $TMPDIR"
disp "Log File Name: $LOGFILENAME"
disp "Log File Full Path: $LOGFILE"
disp "Test (no change) : $TEST"
disp "Verbose : $VERBOSE"
}
function main() {
#debug print input parameters
input_show
#validate input parameters
input_check
disp "Rotating log file '$LOGFILE'" -s
agefile.sh "$LOGFILE"
#Find channels tags with matching sort index
LIST=$(curl -s --data-urlencode filter=\[\{\"field\":\"index\",\"type\":\"numeric\",\"value\":\"${lookupsortindex}\",\"comparison\":\"EQ\"\}\] -d start=0 -d sort=index -d dir=desc -d all="${inc_disabled}" "http://$uid:$pwd@$host:$port/api/channeltag/grid" | jq -r "$search")
if [[ $? == 0 ]] ;
then
disp "Curl Channel Search for Index '"$lookupsortindex"' Succeeded"
else
disp "Curl Channel Search for Index '"$lookupsortindex"' Failed"
exit 1
fi
TAGSFOUND=$(echo $LIST|wc -w)
if [[ "$TAGSFOUND" -gt "0" ]];
then
if [[ "$TEST" == "0" ]] ;
then
disp "'"$TAGSFOUND"' Channel Tags to be updated"
for UUID in $LIST
do
CMD=$(curl -s --data-urlencode node=\{\"uuid\":\"${UUID}\",\"index\":\"${replacesortindex}\",\"comment\":\"${comment}\"\} "http://$uid:$pwd@$host:$port/api/idnode/save")
if [[ $? == 0 ]] ;
then
disp "Curl Channel Update Succeeded for: '"$UUID"'"
else
disp "Curl Channel Update Failed for: '"$UUID"'"
fi
done
else
disp "TEST MODE - None of following the '"$TAGSFOUND"' Channel Tags will be updated:"
disp "'$(echo $LIST)'"
fi
else
disp "No Channel Tags to Update"
fi
}
#run main program
disp "*** Channel Tag Sort Index Update Started "
main
disp " Channel Tag Sort Index Update Completed ***"
#EXIT SUCCESSFULLY
exit 0
`