How to disable/enable an extension using the asterisk API?

Hello,

I have a need to dynamically disable/enable extensions using the API in PHP?

I have been using the API for years using it to change an extensions caller id and name and redirecting calls but i am unable to find a way to disable/enable extensions.

my thoughts were to simply change the password of the extension but i have not been successful in doing so while using the API.

Any help on this would be appreciated.

Thank You!

What API are you referring to?

The Asterisk Manager API. (see code snippet below)

// Login
$socket = fsockopen($domain, 5038, $errno, $errstr, 5);
fputs($socket, “Action: Login\r\n”);
fputs($socket, “Username: “.$username.”\r\n”);
fputs($socket, "Secret: ".$password.“r\n\r\n”);
$wrets = fread($socket, 8192);

// Set Caller ID
$family=‘AMPUSER’; $key=“100”; $value=“6095551212”;
$astdb_query=“Command: database put “.$family.” “.$key.”/outboundcid “”.$value.”"";
fputs($socket, “Action: Command\r\n”);
fputs($socket, $astdb_query."\r\n\r\n");
$wrets = fread($socket, 8192);

// Logoff
fputs($socket, “Action: Logoff\r\n\r\n”);
$wrets = fread($socket, 8192);
fclose($socket);

Thank You!

Asterisk itself using AMI does not provide a way to enable or disable extensions. What you’ve done there is update some FreePBX specific Asterisk database entry to update the callerid. I doubt there’s any database entries you could change to enable or disable extensions.

this code updates asterisk directly and not FreePBX although we are running FreePBX.
thank you for taking a look though.

It’s updating a database entry that FreePBX created and uses in the Asterisk database, it’s not updating Asterisk itself. Asterisk doesn’t itself use that database entry. Only the logic that FreePBX has in place does.

I appreciate your input but I can tell you that I have been doing this for years on systems with and without FreePBX installed. Thanks Again.

It is entirely possible that you are not running FreePBX. However, for that code to function to modify the extension settings, it must be using dialplan (extensions.conf) that was either modelled after or borrowed from FreePBX. The AMPUSER key is what FreePBX uses to modify extension settings in the AstDB.

To provide the enable/disable function you desire, you will likely need to adjust the dialplan further to check an additional activation flag. There may be other ways of accomplishing what you want as well. However, what you seek to do is not an integral feature provided by Asterisk, short of rewriting the sip or pjsip configuration files to remove an endpoint.

thanks for the feedback. Its odd that the code works as we have been using it on a machine that never had FreePBX installed. that said I need to double check a few things then. Much appreciated!

I should then ask if you know a way via the AMI API to set an extensions outbound caller ID without using the AMPUSER key? I know how to change the caller id on an active channel as we currently do this prior to redirecting a channel. Again, your help is really appreciated.

On a side note, did you see where I asked about changing the extension password using the AMI API? this would effectively disable the extension. The only method I see now, but have not tested, is possibly using the UpdateConfig Action.

http://www.asteriskdocs.org/en/2nd_Edition/asterisk-book-html-chunk/asterisk-APP-F-42.html

Thanks Again.

When you say extension password, what specific concept in Asterisk - not FreePBX - are you referring to?

An Asterisk extension has no concept of a ‘password’. You may implement such a concept using certain dialplan applications, but it is not an intrinsic part of Asterisk.

its the extensions authentication username and password used for sip registration. thank you.

No it is the device authentication password. There are no Asterisk extensions in sip.conf. (JColp has silently compensated for your use of FreePBX terminology.)

Thank You for the clarification
So then my question is do you know a way via the API to change this?
Thank you all for the assistance

Get Outlook for iOS

You could explore using ARI Push Configuration if you are running Asterisk 13.

https://wiki.asterisk.org/wiki/display/AST/ARI+Push+Configuration

thanks for the idea. I will definitely investigate this.

Hello, I am working with the API Action UpdateConfig which will allow me to directly update an asterisk conf file. This should allow me to dynamically make the changes that I need but am not finding success as with the following code I am getting a “Message: Save of config failed”. If anyone can review the following code to see if they can suggest what may be the issue it would be greatly appreciated. Thank you all in advance.

// Login
$socket = fsockopen($domain, 5038, $errno, $errstr, 30);
fputs($socket, “Action: Login\r\n”);
fputs($socket, “Username: “.$username.”\r\n”);
fputs($socket, “Secret: “.$secret.”\r\n\r\n”);
$wrets = fread($socket, 8192);
echo $wrets;

// Update Config
fputs($socket, “Action: UpdateConfig\r\n”);
fputs($socket, “SrcFilename: pjsip.auth.conf\r\n”);
fputs($socket, “DstFilename: /tmp/pjsip.test.conf\r\n”);
fputs($socket, “Action-000000: Update\r\n”);
fputs($socket, “Cat-000000: 10001-auth\r\n”);
fputs($socket, “Var-000000: password\r\n”);
fputs($socket, “Value-000000: newpassword\r\n”);
//fputs($socket, “Reload: Yes\r\n\r\n”);
$wrets = fread($socket, 8192);
echo $wrets;

// Logoff
fputs($socket, “Action: Logoff\r\n\r\n”);
$wrets = fread($socket, 8192);
fclose($socket);
echo $wrets;