I’m new here, and I would like to share my first script on asterisk. Indeed, my script will allow you to create users on pjsip.conf.
It works but on the other hand when it starts, it shows me some errors. Anyone know why I’m making this mistake?
Attached are my script and screenshots
Declaration of variables
general=[general]
selectedContext=businessA # To be modified according to the name of your context created in extensions.conf
template=[template_$selectedContext]
transudp=[transport-udp]
locationFilePjsip=/etc/asterisk/pjsip.conf
echo " " >> $locationFilePjsip
Reads the contents of the file pjsip.conf
cat $locationFilePjsip | while read line
do
# Checks for the presence of strings
if [ $line = $general ]
then
echo “context [general] already present”
# Adds information if it is not present
else
echo "[general]" > $locationFilePjsip
echo "hasvoicemail = yes" >> $locationFilePjsip
echo " " >> $locationFilePjsip
fi
if [ $line = $template ]
then
echo "context [template_$selectedContext] already present"
else
echo "[template_$selectedContext]" >> $locationFilePjsip
echo "dtfmfmode = rfc2833" >> $locationFilePjsip
echo " " >> $locationFilePjsip
fi
if [ $line = $transudp ]
then
echo "context [transport-udp] already present"
else
echo "[template-udp]" >> $locationFilePjsip
echo "type = transport" >> $locationFilePjsip
echo "protocol = udp" >> $locationFilePjsip
echo "bind = 0.0.0.0" >> $locationFilePjsip
echo " " >> $locationFilePjsip
fi
done
echo How many users do you want to create?
read numberOfUsers
Initialize i a 0 for user creation
i=‘0’
while [ “$i” != “$numberOfUsers” ]
do
echo Please enter a phone number
read number
echo Please enter the name of the person
read name
echo Please enter a password for the user
# Hide the password entry
stty -echo
read password
# The character entry is displayed again
stty echo
echo Please enter the context for your user
read context
echo Please enter your voicemail number
read voicemail
#Saves the information entered by the user in the pjsip.conf file
echo " " >> $locationFilePjsip
echo [$number] >> $locationFilePjsip
echo fullname = $name >> $locationFilePjsip
echo " " >> $locationFilePjsip
echo [$number] >> $locationFilePjsip
echo type = aor >> $locationFilePjsip
echo max_contacts = 1 >> $locationFilePjsip
echo " " >> $locationFilePjsip
echo [$number] >> $locationFilePjsip
echo type = auth >> $locationFilePjsip
echo username = $number >> $locationFilePjsip
echo password = $password >> $locationFilePjsip
echo " " >> $locationFilePjsip
echo [$number] >> $locationFilePjsip
echo type = endpoint >> $locationFilePjsip
echo context = $context >> $locationFilePjsip
echo disallow = all >> $locationFilePjsip
echo allow = ulaw >> $locationFilePjsip
echo allow = alaw >> $locationFilePjsip
echo direct_media = no >> $locationFilePjsip
echo mailboxes = $voicemail >> $locationFilePjsip
echo auth = $number >> $locationFilePjsip
echo outbound_auth = $number >> $locationFilePjsip
echo aors = $number >> $locationFilePjsip
echo " " >> $locationFilePjsip
i=$(($i + 1))
done
echo You have created $i user"(s)"
exit 0
I hope I didn’t make a mistake in the category and I thank you for taking the time to help me
I modified my script. Instead of checking if the general context is present, it asks the user if he wants to create it or not. The same applies to other contexts. It was easier to do it like that and it doesn’t create mistakes.
Thanks again to sedwards for these advices
Here are the changes:
Declaration of variables
selectedContext=businessA # To be modified according to the name of your context created in extensions.conf
template=[template_$selectedContext]
locationFilePjsip=/etc/asterisk/pjsip.conf
Creating of the general context
echo “Do you want a general context in your pjsip.conf?”
select i in yes no; do
if [ “$i” = “yes” ]; then
(
echo “[general]”
echo “hasvoicemail = yes”
echo
) >> $locationFilePjsip
echo “Your general context has been added in your pjsip configuration!”
echo
break
elif [ “$i” = “no” ]; then
echo “OK! Next step.”
echo
break
else
echo “Please choose between 1 and 2!”
fi
done
Creating of the template according to the SelectedContext variable
echo “Do you want a template $selectedContext context in your pjsip.conf?”
select i in yes no; do
if [ “$i” = “yes” ]; then
(
echo “[template_$selectedContext]”
echo “dtfmfmode = rfc2833”
echo
) >> $locationFilePjsip
echo “Your template $selectedContext context has been added in your pjsip configuration!”
echo
break
elif [ “$i” = “no” ]; then
echo “OK! Next step.”
echo
break
else
echo “Please choose between 1 and 2!”
fi
done
Creating the transport_udp context
echo “Do you want a transport-udp context in your pjsip.conf?”
select i in yes no; do
if [ “$i” = “yes” ]; then
(
echo “[transport_udp]”
echo “type = transport”
echo “protocol = udp”
echo “bind = 0.0.0.0”
echo
) >> $locationFilePjsip
echo “Your transport-udp context has been added in your pjsip configuration!”
echo
break
elif [ “$i” = “no” ]; then
echo “OK! Next step.”
echo
break
else
echo “Please choose between 1 and 2!”
fi
done
echo “How many users do you want to create?”
read numberOfUsers
Initialize i a 0 for user creation
i=‘0’
while [ “$i” != “$numberOfUsers” ]
do
echo “Please enter a phone number”
read number
echo
echo “Please enter the name of the person”
read name
echo
echo “Please enter a password for the user”
Hide the password entry
stty -echo
read password
The character entry is displayed again
stty echo
echo
echo “Please enter the context for your user”
read context
echo
echo “Please enter your voicemail number”
read voicemail
echo
Saves the information entered by the user in the pjsip.conf file