⬆️ ⬇️

Downloading Phonebooks to Polycom IP Phones



Our organization uses ip telephony based on Trixbox (essentially the same FreePBX with some differences), as well as telephone sets from Polycom. Every day the company's staff grew and it became harder to remember a bunch of internal numbers of employees. Some of the employees started manually entering the address book in the phone, but more than half of it spent tons of paper on a printout of the next changed directory, as well as more time searching for the necessary number. It was necessary to do something with it.

All phones get their configuration through the tftp server, and go there. It was found that when downloading, the phone checks the presence of * macaddr * -directory.xml in the * tftpserver_dir * / polycom / contacts / directory, and loads it if it finds one. We will use this.



Form an action plan:

  1. Formation of the xml file with contacts. In order not to fence services, it is necessary to achieve the formation of bash scripts.
  2. You need to get information about existing phones from the asterisk CLI. # Problem number 1.
  3. Download directories by rebooting phones 1 time per night.




Problem number 1
By default, trixbox, when writing information to the asterisk config file, does not write the caller id anywhere, but stores it only in its mysql database. Based on this, we see the following picture:

trixbox1*CLI> sip show peer 114 trixbox1*CLI> * Name : 114 Secret : <Set> MD5Secret : <Not set> Context : from-internal Subscr.Cont. : <Not set> Language : ru AMA flags : Unknown Transfer mode: open CallingPres : Presentation Allowed, Not Screened Callgroup : Pickupgroup : Mailbox : 114@device VM Extension : *97 LastMsgsSent : 0/0 Call limit : 50 Dynamic : Yes Callerid : "device" <114> MaxCallBR : 384 kbps Expire : 1410 Insecure : no Nat : Always ACL : Yes T38 pt UDPTL : No CanReinvite : No PromiscRedir : No User=Phone : No Video Support: Yes Trust RPID : No Send RPID : No Subscriptions: Yes Overlap dial : Yes DTMFmode : rfc2833 LastMsg : 0 ToHost : Addr->IP : 192.168.0.95 Port 5060 Defaddr->IP : 0.0.0.0 Port 5060 Def. Username: 114 SIP Options : (none) Codecs : 0x28000c (ulaw|alaw|h263|h264) Codec Order : (ulaw:20,alaw:20) Auto-Framing: No Status : OK (18 ms) Useragent : PolycomSoundPointIP-SPIP_321-UA/3.1.3.0507 Reg. Contact : sip:114@192.168.0.95 




We are interested in the string Callerid: “device” <114> Instead of the information about the CallerID (name) of the employee, we see the ill-fated “device”.

You can solve in two ways. The first one is found in the MySQL database where it stores information on CallerID and, on the fly, when generating XML, receive data through queries. Or the second less labor-intensive and less resource-intensive.

Knowing that the trixbox entirely in php, we can try to find in the source code where the trixbox writes this “device”. Picked up a bit in / var / www / find the file /var/www/html/admin/modules/core/functions.inc.php with a very interesting comment from the developers.

 // Very bad $iaxfields[] = array($account,'account',$account); $iaxfields[] = array($account,'callerid',(isset($_REQUEST['description']) && $_REQUEST['description'] != '')?$_REQUEST['description']." <".$account.'>':'device'." <".$account.'>'); 


We will not think and look for where the developers take this description, when creating an extension, the required Display name field and just there we enter the full name of the employee, based on this we will correct a little file.

Do not forget about the backup file that suddenly goes wrong or too much hooked, and yes you should not do it right away on the combat server.

Change in line

 $_REQUEST['description'] 


on

 $_REQUEST['name'] 


we get:

 $sipfields[] = array($account,'callerid',(isset($_REQUEST['name']) && $_REQUEST['name'])?$_REQUEST['name']." <".$account.'>':'device'." <".$account.'>'); 


Save, go to the web and resave the extension.

We go to the asterisk CLI and look happy:

 trixbox1*CLI> sip show peer 114 trixbox1*CLI> * Name : 114 Secret : <Set> MD5Secret : <Not set> Context : from-internal Subscr.Cont. : <Not set> Language : ru AMA flags : Unknown Transfer mode: open CallingPres : Presentation Allowed, Not Screened Callgroup : Pickupgroup : Mailbox : 114@device VM Extension : *97 LastMsgsSent : 0/0 Call limit : 50 Dynamic : Yes Callerid : "Ivan Petrov" <114> MaxCallBR : 384 kbps Expire : 1410 Insecure : no Nat : Always ACL : Yes T38 pt UDPTL : No CanReinvite : No PromiscRedir : No User=Phone : No Video Support: Yes Trust RPID : No Send RPID : No Subscriptions: Yes Overlap dial : Yes DTMFmode : rfc2833 LastMsg : 0 ToHost : Addr->IP : 192.168.0.95 Port 5060 Defaddr->IP : 0.0.0.0 Port 5060 Def. Username: 114 SIP Options : (none) Codecs : 0x28000c (ulaw|alaw|h263|h264) Codec Order : (ulaw:20,alaw:20) Auto-Framing: No Status : OK (18 ms) Useragent : PolycomSoundPointIP-SPIP_321-UA/3.1.3.0507 Reg. Contact : sip:114@192.168.0.95 


The problem is solved, it remains only to go through all the extensions and re-save them so that the trixbox rewrote the config files with the correct caller id.



We go further.

Teach asterisk to remotely overload Polycom phones. To do this, add the following lines to the /etc/asterisk/sip_notify.conf file:

 [polycom-check-cfg] Event=>check-sync Content-Length=>0 


Restart asterisk.

After that if in the asterisk CLI

 sip notify polycom-check-cfg 114 


The Polycom telephone set on which extension 114 is registered will reboot.

Next we get a list of registered phones, we need it to remotely reboot them.

 asterisk -rx 'sip show peers' 


we get

 354/354 192.168.0.226 DNA 5060 OK (19 ms) 353/353 192.168.0.108 DNA 5060 OK (15 ms) 352 (Unspecified) DNA 0 UNKNOWN 351 (Unspecified) DNA 0 UNKNOWN 342/342 192.168.0.138 DNA 5061 OK (7 ms) 341/341 192.168.0.138 DNA 5060 OK (7 ms) 


Using grep, we remove the excess (we are only interested in devices that are currently registered in the list with the “OK” status and only separate phone numbers)

  asterisk -rx 'sip show peers' |grep OK |awk '{print $1}'|awk -F'/' '{print $1}' 


we get

 400 363 362 361 359 357 356 355 354 353 342 


Directing the result to a temporary file from there will be easier to read line by line.

  asterisk -rx 'sip show peers' |grep OK |awk '{print $1}'|awk -F'/' '{print $1}' > numbers.txt 


In this list, we received those phones that need to be rebooted and a directory loaded into them.

By the second run, we form the xml file itself (again excluding the unnecessary) and write to the file.

 asterisk -rx 'sip show peers' |egrep 'OK|UNKNOWN' |grep -v 'GSM' > extensions.txt 


Caller id is obtained as follows

 asterisk -rx 'sip show peer 114' |grep Callerid 


Next, we need to read extensions.txt line by line to get the caller id and write to the file forming pieces xml on the fly.

To begin with, let's create a reference 000000000000-directory.xml, it is loaded by phones by default only once when the phone is first loaded, then the file macaddress-directory.xml is created in the / tftpboot / polycom / contacts / folder, which will be constantly loaded later ( forget to replace the current ones after the formation of the reference file so that they are loaded into the phone). Since this xml need headings and structure that the phone understands. We collect:

 echo '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' > 000000000000-directory.xml echo '<!-- $RCSfile$ $Revision: 35928 $ -->' >> 000000000000-directory.xml echo '<directory>' >> 000000000000-directory.xml echo ' <item_list>' >> 000000000000-directory.xml 


Note the last line with a tab, so that the file is beautiful when formed, and structured.

Next reading loop extensions.txt

 cat extensions.txt | while read line do 


First we get rid of all the excess (faxes, free extensinons, spare numbers starting with 1000x, etc.)

 RES=$(asterisk -rx "sip show peer $line" |grep Callerid |grep -v 'Free' |grep -v 'free' |grep -v 'fax' |grep -v '1000' |grep -v 'device' |grep -v 'FAX' |grep -v 'Test'); if [ "$RES" != "" ]; then 


and here we are forming a beautiful piece of xml code

 asterisk -rx "sip show peer $line" |grep Callerid | awk -F': "' '{print $2}' | awk -F'" <' '{print \ "\t\t<item>\n\t\t\t<ln>"$1"</ln>\n\t\t\t<ct>"$2"</ct>\n\t\t</item>"}' >> 000000000000-directory.xml 


example

  <item> <ln>Ivan Petrov</ln> <ct>114></ct> </item> 


well and we add structure xml

 echo ' </item_list>' >> 000000000000-directory.xml echo '</directory>' >> 000000000000-directory.xml 


The complete code for generating the xml file:

 echo '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' > 000000000000-directory.xml echo '<!-- $RCSfile$ $Revision: 35928 $ -->' >> 000000000000-directory.xml echo '<directory>' >> 000000000000-directory.xml echo ' <item_list>' >> 000000000000-directory.xml cat extensions.txt | while read line do RES=$(asterisk -rx "sip show peer $line" |grep Callerid |grep -v 'Free' |grep -v 'free' |grep -v 'fax' |grep -v '1000' |grep -v 'device' |grep -v 'FAX' |grep -v 'Test'); if [ "$RES" != "" ]; then asterisk -rx "sip show peer $line" |grep Callerid | awk -F': "' '{print $2}' | awk -F'" <' '{print \ "\t\t<item>\n\t\t\t<ln>"$1"</ln>\n\t\t\t<ct>"$2"</ct>\n\t\t</item>"}' >> 000000000000-directory.xml fi done echo ' </item_list>' >> 000000000000-directory.xml echo '</directory>' >> 000000000000-directory.xml 


All Polycom devices have a mac-address starting at 00: 04: F, from here you can find all the phones on the network via arp

 arp -a |grep 00:04:F |awk '{print $4}' > mac.txt 


cycle copy 000000000000-directory.xml to macaddress-directory.xml

 cat mac.txt |while read phonemac do MAC=${phonemac//:/}; #     ":"   cp /tftpboot/polycom/contacts/000000000000-directory.xml /tftpboot/polycom/contacts/$MAC-directory.xml done 


Well, almost the finish line in a cycle from the numbers.txt file obtained at the beginning by rebooting the phones:

 cat numbers.txt |while read number do asterisk -rx "sip notify polycom-check-cfg $number" done 


Full script code:

 #!/bin/sh asterisk -rx 'sip show peers' |grep OK |awk '{print $1}'|awk -F'/' '{print $1}' > numbers.txt asterisk -rx 'sip show peers' |egrep 'OK|UNKNOWN' |grep -v 'GSM' |awk '{print $1}'|awk -F'/' '{print $1}' > extensions.txt arp -a |grep 00:04:F |awk '{print $4}' > mac.txt echo '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' > 000000000000-directory.xml echo '<!-- $RCSfile$ $Revision: 35928 $ -->' >> 000000000000-directory.xml echo '<directory>' >> 000000000000-directory.xml echo ' <item_list>' >> 000000000000-directory.xml cat extensions.txt | while read line do RES=$(asterisk -rx "sip show peer $line" |grep Callerid |grep -v 'Free' |grep -v 'free' |grep -v 'fax' |grep -v '1000' |grep -v 'device' |grep -v 'FAX' |grep -v 'Test'); if [ "$RES" != "" ]; then asterisk -rx "sip show peer $line" |grep Callerid | awk -F': "' '{print $2}' | awk -F'" <' '{print "\t\t<item>\n\t\t\t<ln>"$1"</ln>\n\t\t\t<ct>"$2"</ct>\n\t\t</item>"}' >> 000000000000-directory.xml fi done echo ' </item_list>' >> 000000000000-directory.xml echo '</directory>' >> 000000000000-directory.xml cp 000000000000-directory.xml /tftpboot/polycom/contacts/000000000000-directory.xml cat mac.txt |while read phonemac do MAC=${phonemac//:/}; cp /tftpboot/polycom/contacts/000000000000-directory.xml /tftpboot/polycom/contacts/$MAC-directory.xml done cat numbers.txt |while read number do asterisk -rx "sip notify polycom-check-cfg $number" done rm numbers.txt rm extensions.txt rm mac.txt rm 000000000000-directory.xml 


We save the script in the /etc/cron.daily folder and after that every night all changes will be uploaded to the phone directory of the desk phone with the ability to quickly search it. Users are satisfied, paper in the office is not in vain translated.


')

Source: https://habr.com/ru/post/211606/



All Articles