Need help with an Asterisk create user bash script


#1

Hi guys,

I am trying to create a bash script to save myself some manual work making asterisk users. My bash is pretty shaky however and I have gotten as far as I can without the need of pouring over my scripting cookbooks this weekend. However I was wondering if anyone could make any suggestions for improvements in this? Many thanks if you can help and bonus points for insulting my lack of scripting knowhow :smile:

[code]#!/bin/bash

EXT=$1
SECRET=$2
NAME=$3
EMAIL=$4
LOC=$5

args=("$@")
echo ${args[0]} ${args[1]} ${args[2]} ${args[4]} ${args[5]}

function writetosip {

echo -n “$EXT”\n
"secret=$SECRET"\n
"callerid=$NAME <$EXT>"\n

/etc/asterisk/sip.conf; # I know this is not working as I want it to
}

function writetovoicemail {

echo -n “$EXT=> $EXT, $NAME, $EMAIL,attach=yes|tz=pacific|delete=yes” >>
/etc/asterisk/voicemail.conf; # Ditto for this not working as I want it to

}

echo “Writing to sip…”;
writetosip;

echo “Writing to voicemail.conf…”;
writetovoicemail;

#echo asterisk -vvvvr ; sip reload; voicemail reload; commented out since i’m not testing on an asterisk box

if [ “$1” = “-h” -o “$1” = “–help” ] # Request help.
then
echo “usage: ./create_phone EXT Secret First_Name Last_Name Email_Address Location (airport code, remote)”;
fi[/code]


#2

In the writetosip function, I would suggest…

echo “$EXT
secret=$SECRET
callerid=$NAME <$EXT>
” >> /etc/asterisk/sip.conf

The double quote protects the embedded new lines, so you do not want the \n inserted manually. I also removed the -n switch because why remove an automatic new line and then add one in manually. As written, it will create a blank line between sip device entries which will improve readability of the file later.

In the writetovoicemail() function;

I do not see anything immediately except the redirect pathname is on a different line than the echo command, however I do not know if this is the way it was written or a side effect of being posted. Because of the way the shell processes redirect, the “>>” and the pathname need to be on the same line as the command.

I would also suggest removing the -n for the same reason I stated above.

When you do go live with the script, your asterisk line does not look like it would work.
Try…
asterisk -rx "sip reload"
asterisk -rx “voicemail reload”

Finally, you have your help section after you have already written the sip and voicemail lines. This will cause the file to get garbage in them. Place the help before the calls to writetosip and writetovoicemail and then exit the script .

See if that helps.


#3

If you’re doing this for a bunch of users on a regular basis, it might be better to look into using a database-backed realtime setup. Set it alll up and when you need to add users, you throw a row or two into a database and bang you’re done. You don’t even have to tell Asterisk to reload.


#4

That is also a good tip, thanks. My new working version is below, I will test it later today, I think it will work now!

Thoughts to improve it for the next version includes passing the secret to a generate random password script to fill that value in automatically.

[code]#!/bin/bash

EXT=$1
SECRET=$2
NAME=$3
EMAIL=$4
LOC=$5

if [ “$1” = “-h” -o “$1” = “–help” ] # Request help.
then
echo “usage: ./create_sip_user EXT Secret “First_Name Last_Name” Email_Address Location (location, remote)”;
exit 1;
fi

args=("$@")
echo ${args[1]} ${args[2]} ${args[3]} ${args[4]} ${args[5]}

function writetosip {

echo “$EXT
secret=$SECRET
callerid=$NAME <$EXT>
” >> /etc/asterisk/sip.conf
}

function writetovoicemail {

echo “$EXT=> $EXT, $NAME, $EMAIL,attach=yes|tz=pacific|delete=yes”\n >> /etc/asterisk/voicemail.conf;

}

echo “Writing to sip…”;
writetosip;

echo “Writing to voicemail.conf…”;
writetovoicemail >> /etc/asterisk/voicemail.conf;

echo asterisk -vvvvrx sip reload; asterisk -vvvvrx voicemail reload;[/code]


#5

That looks good, but I have a couple of comments.

In your call to writetovoicemail you redirect the output the file, and you also do the same within the function. The redirect on the call to the function is not necessary.

Also I have a feeling you are going to have problems with the reload commands.
You I think you probably will need to quote the command to the Asterisk, though I have never tried it without quotes.

asterisk -vvvrx sip reload

becomes

asterisk -vvvrx “sip reload”