Script to create users in the pjsip.conf file

Hello everyone,

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 :slight_smile: error%20when%20running%20the%20script

It looks like you have some quoting problems with all of the unexpected operator errors.

1 Like

Hello, I solved the problem.

There were missing quotes for

if [ $line = $general ]

That’s what we had to do:

if [ “$line” = “$general” ]

Sorry for the inconvenience and thank you!

Have a nice day!

Just a couple of ‘drive-by’ suggestions on your shell scripting…

  1. You can ‘prompt and ask’ in a single line. For example, instead of:
        echo Please enter a password for the user

        # Hide the password entry
        stty -echo
        read  password

        # The character entry is displayed again
        stty echo

try:

        prompt -p 'Please enter a password for the user: ' -s password
  1. You can output a ‘block’ or even the entire configuration in a single ‘write.’ For example, instead of:
        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

try:

	    (
        echo
        echo [$number]
        echo fullname = $name
        echo
        echo [$number]
        echo type = aor
        echo max_contacts = 1
        echo
        ) > $locationFilePjsip

much ‘cleaner,’ efficient, and easier to maintain.

2 Likes

Hello,

The prompt command does not exist when I type it on debian.

It displays " command not found"

However, your second suggestion works.

I thank you for that.

If you have any other advice to give me, don’t hesitate to contact me

Have a nice day!

Hello everyone,

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

    	(

echo
echo [$number]
echo fullname = $name
echo
echo [$number]
echo type = aor
echo max_contacts = 1
echo
echo [$number]
echo type = auth
echo username = $number
echo password = $password
echo
echo [$number]
echo type = endpoint
echo context = $context
echo disallow = all
echo allow = ulaw
echo allow = alaw
echo direct_media = no
echo mailboxes = $voicemail
echo auth = $number
echo outbound_auth = $number
echo aors = $number
echo
) >> locationFilePjsip i=(($i + 1))
done
echo You have created $i user"(s)"
exit 0