#!/bin/bash # Hangup channels whose duration exceeds a timeout # It's also possible to handle only a specific peer's channels VERBOSE=0 DEBUG=0 PEER="" HANGUP_TIMEOUT="" function usage() { [ -n "$1" ] && echo "$1" && echo echo "Usage : $0 [-cX] [-wX] [-d] [peer]" echo " -h Show this help message" echo " -v Enable verbose mode" echo " -d Enable debug mode" echo " -t[X] Specify hangup timeout (in s)" echo " [peer] Specify the peer name" } function log() { case "$1" in INFO) [ $VERBOSE -ne 1 -a $DEBUG -ne 1 ] && return ;; DEBUG) [ $DEBUG -ne 1 ] && return ;; esac echo -e "$( date "+%Y/%m/%d %H:%M:%S" ) - $2" } # Parse arguments for arg in $@ do if [ $( echo $arg|egrep -c '^-[tvdh][0-9]*$' ) -gt 0 ] then a=$( echo $arg|sed 's/^-\([tvdh]\).*$/\1/' ) v=$( echo $arg|sed 's/^-[tvdh]//' ) case $a in t) HANGUP_TIMEOUT=$v ;; v) VERBOSE=1 ;; d) DEBUG=1 ;; h) usage exit 0; ;; esac elif [ -z "$PEER" ] then PEER="$arg" else echo "Only one peer can be specified (or invalid '$arg' parameter)" fi done [ -z "$HANGUP_TIMEOUT" ] && usage "You must specify the hangup timeout using -t parameter." && exit 1 if [ -n "$PEER" ] then log DEBUG "Specified peer name : $PEER" CHANNELS=$( asterisk -rx 'core show channels concise'|grep -E "^$PEER-[0-9a-f]+!" ) log DEBUG "Channels :\n$CHANNELS" [ -z "$CHANNELS" ] && log INFO "No current channels found for this peer" && exit 0 else CHANNELS=$( asterisk -rx 'core show channels concise'|grep '!' ) log DEBUG "Channels :\n$CHANNELS" [ -z "$CHANNELS" ] && log INFO "No current channels found" && exit 0 fi total=0 count=0 IFS=" " for line in $CHANNELS do let total=total+1 chan=$( echo "$line"|cut -d'!' -f 1 ) duration=$( echo "$line"|cut -d'!' -f 12 ) log DEBUG "Channel '$chan' duration : ${duration}s" [ $duration -le $HANGUP_TIMEOUT ] && log DEBUG "This channel duration do not exceed the timeout." && continue log INFO "Channel '$chan' duration is ${duration}s : hangup this channel" asterisk -rx "hangup request $chan" let count=count+1 done log INFO "$count/$total channels hangup"