Last active
April 9, 2021 09:43
-
-
Save mammuthus/3530e4ce6790c668aac97c872b9eac2d to your computer and use it in GitHub Desktop.
RussianPost: sip config maker for Asterisk and FreeBSD
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/local/bin/bash | |
# Asterisk sip config maker for FreeBSD | |
# echo 'alias an="/usr/local/etc/asterisk/spbpost/an.sh"' >> ~/.bashrc && . ~/.bashrc | |
WONOK='\033[37;1;42m' | |
WONWARN='\033[37;1;46m' | |
WONERR='\033[37;1;41m' | |
NORMAL='\033[0m' | |
Secret=$(date +%s | md5 | head -c 6) # Hash a current unixtime and cut it - "md5" util for *BSD | |
Tmp="/usr/local/etc/asterisk/spbpost/an_num.tmp" # Temporary file path to save previous Number value | |
Tmpg=$(cat $Tmp) | |
Tmpi=$(expr $Tmpg + 1) # Autoincrement to prefill Number field next round | |
echo; printf "${WONOK} ${NORMAL} Последние созданные файлы \\n" | |
ls -t1 | sed /^total/d | head -5; echo # Just cuts ls | |
printf "${WONWARN} ${NORMAL} Введите номер абонента > %s" | |
read -i "$Tmpi" -e Number; echo | |
echo "$Number" > "$Tmp" # Number field=1 if first round, else =$Tmp | |
printf "${WONWARN} ${NORMAL} Введите имя как в AD %s\\n" | |
read -rp " Например Daniil.Mitrofanov > " Name; echo | |
printf "${WONWARN} ${NORMAL} Введите контекст %s\\n" | |
read -rp " Или нажмите Ввод для контекста ufps > " Context; echo | |
if test -z "$Context" | |
then Context="ufps" # Default context is "ufps" | |
fi | |
Path="/usr/local/etc/asterisk/spbpost/${Number}" # User config path | |
ShortPath="spbpost/${Number}" # User config path | |
Sipconf="/usr/local/etc/asterisk/sip.conf" # sip.conf path obviously | |
Md5=$(echo -n "$Number:asterisk:$Secret" | md5) # Hash user:asterisk:secret not to store secret in plaintext - "md5" util for *BSD | |
# Write to out file | |
AD="/media/sharefolders/out" # Target file | |
intNumber="${Number:0:1}-${Number:1:3}-${Number:4:4}" | |
longNumber="+7 (812) 616-03-53,,${Number:4:4}" | |
echo "$Name;$intNumber;$longNumber" >> "$AD" | |
# Start | |
if test -f "$Path" | |
then printf "${WONERR} ОШИБКА ${NORMAL} Такой файл уже существует %s\\n"; echo | |
else # Creating user config file | |
touch "$Path" | |
echo "[$Number](sets)" >> "$Path" | |
echo "username=$Number" >> "$Path" | |
echo "md5secret=$Md5" >> "$Path" | |
echo "dial=SIP/$Number" >> "$Path" | |
echo "mailbox=$Number@device" >> "$Path" | |
echo "callerid=\"$(echo $Name | tr -s '._-' ' ')\" <$Number>" >> "$Path" # Replace dot/hyphen/underscore with space | |
echo "context=$Context" >> "$Path" | |
echo "callgroup=4" >> "$Path" | |
echo "pickupgroup=4" >> "$Path" | |
if test -f "$Path" | |
then printf "${WONOK} УСПЕХ ${NORMAL} Файл $Path создан %s\\n" | |
# sed -i "/\;This/i #include \"$Path\"" $Sipconf # GNU sed only | |
# POSIX sed sucks | |
cr=" | |
" | |
sed -i -e "/\;This/i\\ | |
#include \"$ShortPath\"${cr}" $Sipconf # Search for ";This"-marker, add include string before it | |
#End of POSIX sed magic | |
if [ $? -ne 0 ]; # Better to fix this with "if mycmd" | |
then printf "${WONERR} ОШИБКА ${NORMAL} Файл /${Number} не добавлен в sip.conf %s\\n" | |
else printf "${WONOK} УСПЕХ ${NORMAL} Файл /${Number} добавлен в sip.conf %s\\n" | |
printf "${WONWARN} ИМЯ ${NORMAL} $Number %s"; echo | |
printf "${WONWARN} ПАРОЛЬ ${NORMAL} $Secret %s"; echo | |
fi | |
asterisk -rx 'sip reload' | |
if [ $? -ne 0 ]; # Check the return value (better to fix this with "if mycmd") | |
then | |
printf "${WONERR} ОШИБКА ${NORMAL} Sip reload не завершен %s\\n"; echo | |
else | |
printf "${WONOK} УСПЕХ ${NORMAL} Sip reload завершен %s\\n"; echo | |
fi | |
else printf "${WONERR} ОШИБКА ${NORMAL} Файл не создан %s\\n"; echo | |
fi | |
# Logging new users | |
Log="/usr/local/etc/asterisk/spbpost/an_log.csv" # Log path | |
echo "$(date),$Name,$Number,$Secret" >> "$Log" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment