How to import data into asterisk Database

Hello All,

I have lots of data to import into the asterisk database. Anyone know a quick and fast way to do it ?

Thx

do you mean the ASTDB or a DB that your Asterisk system/dialplan uses ?

ASTDB

Anyone know of a way to do this?

I want to use LookupCIDName to work out the name of incomming callers based on their CID number. Unfortunately, I have several hundred names and numbers to enter so ‘database put cidname callername callernumber’ is not really an option.

Any ideas?

i use an AGI to lookup the details in a MySQL db table. if you want a copy, drop me a PM with your email addy.

you’ll need to work out the inserting though, all of the ones i have here are part of a bigger system so probably need as much work as starting from scratch to make them work. i’ve got a click-2-call web interface somewhere too.

Thanks for the offer, BLT, but I’d rather work with what I have. In the end, I may have to learn about MySQL too but, in the mean time, I’ll hang on in hope of finding a simpler solution.

I think the fastest way to import into the ASTDB is to create a Bash script with lines similar to:

asterisk -rx database put XXX YYY
asterisk -rx database put WWW ZZZ
etc.

Then just make the Bash script executable and run it.

I thought of something very similar in bed last night. How about this then:

It should be well within my capabilities to make a text file, say import.txt, that contains the lines:

database put cidname 01234567890 "A Caller"
database put cidname 01234567891 "A N Other"
etc

Then just start up the asterisk console with:
$ asterisk -r < import.txt

I’m new to linux, so it’s best to keep things as simple as possible. What d’ya think?

that could work. i’ve actually never tried redirecting from a text file to the Asterisk CLI so don’t know the syntax, but conceptually you’re on the right track. if what you suggest doesn’t work, then do it my way. if you don’t understand “my way” due to Linux limitations, let me know and i’ll lend you a hand.

do it my way. contents of bash script:

#!/bin/bash asterisk -rx "database put calll 4546 'jon holmes'" asterisk -rx "database put calll 334 'fred jones'"

then make the script executable on the Linux shell:

chmod +x scriptname

then run the script:

./scriptname

Hi

If its a list then it sould be scripable. If its single entries that you want to update then PHP will do it easily. I use this method to set a lot of functions in the ASTDB.

Well Asterisk seems to be very sensitive when it comes to scripts!

First, I tried my way, that is:
$ asterisk -r < import.txt
where import.txt just contains lines like:
database put cidname 01234567890 “A Customer”

All asterisk did was kept outputting its command prompt over and over until I control-C’ed out of it. The database wasn’t updated.

===

Then I tried gtcleaves way with:
$ ./import.txt
where import.txt is executable and contains:
#!/bin/bash
asterisk -rx “database put cidname 01234567890 ‘A Customer’”
…etc

All I get is:
: bad interpreter: No such file or directory

===

Then I tried another way with:
$ bash import.txt
where import.txt contains:
asterisk -rx “database put cidname 01234567890 ‘A Customer’”
…etc

All I get is:
Usage: database put
Adds or updates an entry in the Asterisk database for
a given family, key, and value.
Verbosity is at least 3

===

Then I modified import.txt to swap the quotes round:
$ bash import.txt
where import.txt contains:
asterisk -rx ‘database put cidname 01234567890 “A Customer”’
…etc

Finally, I get lots of:
Updated database successfully
Verbosity is at least 3

===

However, when I look at the database it all appears very weird. The entries that I put in by hand are fine but the ones that were entered via the script seem most peculiar. For example:

localhost*CLI> database show cidname 1132311113 : The Lettershop 01132362366 : G~hnd Box-Leeds 1132362855 : Terry Bambrook 370054 : Jon Gordon /01132391880 : Hawarden Sheeter /01132426999 : Hazel Products L /01132450788 : Powerlite (royds 32452490 : Allied Tapes 1132460410 : White Rose Pkg
Normally, it shows the family, which in this case is ‘cidname’, at the left hand side. This is followed by ‘/’ and then each phone number in full. I’ve tried Putty’ing in from another machine and I get exactly the same output.

I’m not sure whether the scripting is going wrong or there’s just something funny with the ‘database show’ command.

Hi

This is the command I use from php

and that works fine

and use awk and sed to easily replace / print / insert words that would take forever editing by hand…

Well, I have persisted with this and finally got it sorted - phew!

It’s all about bash scripting which seems to be very fussy.

I now have a script called import.txt that starts off like thisasterisk -rx $'database put cidname 01132311113 "The Lettershop"'; asterisk -rx $'database put cidname 01132362366 "G\'hnd Box-Bradfo"'; asterisk -rx $'database put cidname 01132362366 "G\'hnd Box-Leeds"'; asterisk -rx $'database put cidname 01132362855 "Terry Bambrook"'; asterisk -rx $'database put cidname 01132370054 "Jon Gordon"'; asterisk -rx $'database put cidname 01132391880 "Hawarden Sheeter"'; asterisk -rx $'database put cidname 01132426999 "Hazel Products L"'; asterisk -rx $'database put cidname 01132450788 "Powerlite (north"'; asterisk -rx $'database put cidname 01132450788 "Powerlite (royds"';

Note the “dollars-quote” at the front of the asterisk CLI command - this lets us do some ‘escaping’ so that we can have apostrophes in the text name (eg G’hnd Box). Also note the semicolon at the end of each line. This is vital for, without it, bash’s line parsing goes all to cock. It moans that there is no command after each semicolon but it gets the job done.

Run> asterisk -rx ‘database show cidname’ > cid.txt
and hey-presto/cidname/01132311113 : The Lettershop /cidname/01132362366 : G'hnd Box-Leeds /cidname/01132362855 : Terry Bambrook /cidname/01132370054 : Jon Gordon /cidname/01132391880 : Hawarden Sheeter /cidname/01132426999 : Hazel Products L /cidname/01132450788 : Powerlite (royds /cidname/01132452490 : Allied Tapes

I’m happy now :smile: