#!/bin/bash # # Astrandom v4.3, last revised 12/16/2021 # # Script to connect to a random Allstar Node, by K0KN 2/2011 # # Usage = astrandom [your node#] [2] [3] ; 2 = monitor only, 3 = transceive # # The "allstarrandom" and other custom audio files are available via WGET: # wget www.qsl.net/k0kn/k0kn_extra_sounds.tgz # # You can place any node numbers or callsigns that you do not wish to dial in the DENYLIST file specified below, one per line. Do not # leave any blank lines in this file. For example: (do not include the # of course). Also note that K0KN and K0KN-R are considered # different by the deny routine. # # 2000 # 2001 # 29000 # K0KN # W1AW-R selectattempt=3 # Set this number to the maximum number of attempts you would like this script to make CUSTOM=/dev/shm NODELIST="/dev/shm/allstar_nodelist" LOGFILE="/var/log/asterisk/connectlog" DENYLIST="/home/kyle/astrandom_deny" LISTURL=http://stats.allstarlink.org/api/stats/mapData FILENAME1="/dev/shm/astrandom.x1" RANDOMSOUND="/etc/asterisk/sounds/allstarrandom" ERRORSOUND="/var/lib/asterisk/sounds/an-error-has-occurred" retrycount=0 selectcount=0 denynode="" LOCALNODE=$1 if [ "$1" == "" -o "$2" == "" ] ; then echo "Usage = astrandom [your node#] [2] [3]"; exit fi if ! [ -f $DENYLIST ] ; then touch $DENYLIST fi downloadnodelist () { # Create / update local nodelist retrycount=$((retrycount + 1)) if [ $retrycount == 10 ] ; then echo $(date) "Error downloading nodelist" >> $LOGFILE ; echo "Error downloading nodelist -- exiting" ; exit ; fi if ! [ -f $NODELIST ] ; then echo "No nodelist found - downloading" ; wget -O "$NODELIST"2 $LISTURL ; checknodelist ; fi if [ "$(( $(date +"%s") - $(stat -c "%Y" $NODELIST) ))" -gt "3600" ] ; then echo "Nodelist over 1 hour old - downloading new" ; wget -O "$NODELIST"2 $LISTURL ; checknodelist ; fi } checknodelist () { # Remove lat/long fields cut -f1,2,5,6 "$NODELIST"2 > $NODELIST selectrandom } selectrandom () { selectcount=$((selectcount + 1)) denynode="" echo "selectcount: $selectcount" if [ $selectcount -gt $selectattempt ] ; then echo $(date) "Random Allstar script - attempt $selectcount. Maximum $selectattempt. Giving up" >> $LOGFILE asterisk -rx "rpt localplay $LOCALNODE $ERRORSOUND" echo "Attempt $selectcount. Maximum $selectattempt. Giving up" ; exit fi # Generate Random Line # between 1 and number of lines in file RAN_NODE=$[ ( $RANDOM % $LINES ) + 1 ] echo "random number =" $RAN_NODE # Copy Random line to temp file sed -n ""$RAN_NODE"p" "$NODELIST" > "$CUSTOM"/random_node # Parse temp file node_num=$(cut -c 1-5 "$CUSTOM"/random_node) echo "node number =" $node_num echo $node_num > "$CUSTOM"/random_anode_dialed NODENAME=$(cat "$CUSTOM"/random_node) NODENAME=$(echo $NODENAME | awk '{print toupper($0)}') # Resolve node number to callsign from nodelist grep -F $node_num $NODELIST > $FILENAME1 CALLSIGN3=$(cut -c54-63 "$FILENAME1") CALLSIGN2=$(echo "$CALLSIGN3" | tr -d ' ') CALLSIGN=$(echo "$CALLSIGN2" | tr [a-z] [A-Z]) # checkdeny } # checkdeny () { # Check to see if random node callsign is in deny list while IFS= read -r line do echo "Deny node: $line" line=$(echo "$line" | awk '{print toupper($0)}') if [ "$line" = "$NODENAME" -o "$line" = "$node_num" -o "$line" = "$CALLSIGN" ] ; then denynode="deny" echo "Random node in deny list! Denied node - $node_num $CALLSIGN $NODENAME" echo $(date) "Random Allstar script - denied node $node_num $CALLSIGN $NODENAME" >> $LOGFILE fi done < $DENYLIST # if [ "$denynode" = "deny" ] ; then selectrandom fi } # Clean up old temp files if [ -f "$CUSTOM"/random_list ] then rm "$CUSTOM"/random_list fi if [ -f "$CUSTOM"/random_count ] then rm "$CUSTOM"/random_count fi if [ -f "$CUSTOM"/random_node ] then rm "$CUSTOM"/random_node fi asterisk -rx "rpt localplay $LOCALNODE $RANDOMSOUND" # Delete nodelist if zero bytes if ! [ -s $NODELIST ] ; then rm -f $NODELIST ; fi # Run Check/download nodelist routine downloadnodelist ############################# # Count number of stations listed wc -l "$NODELIST" > "$CUSTOM"/random_count LINES=$(cut -d " " -f1 "$CUSTOM"/random_count) LINES=$(( $LINES - 2 )) echo "Number of logged in nodes: $LINES" while read ; do echo "${REPLY%% Y *}" >> "$CUSTOM"/random_list done <"$CUSTOM"/random_count # Select random node and check against denylist selectrandom # Node logging echo $(date) "Random Allstar script - $1 called node $node_num $NODENAME" >> $LOGFILE echo "" # Make Call echo "rpt fun $1 *$2$node_num ($NODENAME)" asterisk -rx "rpt fun $1 *$2$node_num" echo "asterisk -rx rpt fun $1 *$2$node_num" sleep 1 # done exit 0 fi