Initial steps:
Following command will print out details of all active calls:
asterisk -rx "core show channels concise"
# Output:
# SIP/alice-00000002!asterisk.cr!!1!Up!Echo!!!!!3!77!!1572188916.4
Cound delimiters '!' for your asterisk and test with command below, it prints channel name and duration:
asterisk -rx "core show channels concise" | awk -F '!' '{print $1" "$12}'
# Output:
# SIP/alice-00000002 167
Finally the script it will print out asterisk command to hangup call or call duration. Output it to the temp file and execute it, that's all
asterisk -rx "core show channels concise" | awk -F '!' '{print ($12 > 300) ? "asterisk -rx \"hangup request "$1"\"" : "echo \""$1" "$12"\"" }'
# Output:
# asterisk -rx "hangup request SIP/alice-00000002"
asterisk -rx "core show channels concise" | awk -F '!' '{print ($12 > 3000) ? "asterisk -rx \"hangup request "$1"\"" : "echo \""$1" "$12"\"" }'
# Output:
# echo "SIP/alice-00000002 476"
How to use script:
asterisk -rx "core show channels concise" | awk -F '!' '{print ($12 > 300) ? "asterisk -rx \"hangup request "$1"\"" : "echo \""$1" "$12"\"" }' \
> /tmp/tempfile.sh && sh -c /tmp/tempfile.sh; rm /tmp/tempfile.sh