GENWiki

Premier IT Outsourcing and Support Services within the UK

User Tools

Site Tools


source:programatically_delete_extension_from_freepbx

FreePBX programatically Delete Extensions

Sometimes you want to programatically delete endpoints(extensions) from FreePBX at the shell level. This PHP will work just fine.

<?php
// PBXdelete version 1.001b
// Richard taylor 2021
 
if (!isset($argv[1])){
	echo "Usage php pbxdelete.php <ext number>"; exit;
}
 
include '/etc/freepbx.conf';
 
$FreePBX = FreePBX::Create();
$device=$FreePBX->Core->getDevice($argv[1]);
$user=$FreePBX->Core->getUser($argv[1]);
 
if($device["user"]){
	// normal extensions tested with sip, pjsip and dahdi
	echo "Found device ".$argv[1].", deleting...\n";
	echo "Found user ".$device["user"].", deleting...\n";
	$foo=$FreePBX->Core->delDevice($argv[1]);
	$foo=$FreePBX->Core->delUser($device["user"]);
} elseif ($user) {
	// for extensions with users but no device i.e. virtual
	echo "Found user ".$user['extension'].", deleting...\n";
	$foo=$FreePBX->Core->delUser($argv[1]);
} else {
	echo "Neither user nor device ".$argv[1]." exists, exiting...\n";
}
 
?>

But sometimes you have a list, this can be done with…

// PBXdeletelist Version 1.004b
// Richard Taylor 2021
 
if (!isset($argv[1])){
	echo "Usage php pbxdeletelist.php <ext number>,<ext number>,<ext number>"; exit;
}
 
$exts=explode(",",$argv[1]);
 
include '/etc/freepbx.conf';
$FreePBX = FreePBX::Create();
 
 
foreach($exts as $ext) {
 
	$device=$FreePBX->Core->getDevice($ext);
	$user=$FreePBX->Core->getUser($ext);
	echo $ext." "; //"Device ".$argv[1]." User ".$device["user"]." ";
	if($device["user"]){
		// normal extensions tested with sip, pjsip and dahdi
		echo "DU";
		$foo=$FreePBX->Core->delDevice($ext);
		$foo=$FreePBX->Core->delUser($device["user"]);
	} elseif ($user) {
		// for extensions with users but no device i.e. virtual
		echo "U";
		$foo=$FreePBX->Core->delUser($ext);
	} else {
		echo "X";
	}
	echo "\n";
}

In this case we're issue something like

php pbxdeletelist.php 2000,2001,2002,2003,2004,2005

/data/webs/external/dokuwiki/data/pages/source/programatically_delete_extension_from_freepbx.txt · Last modified: 2021/11/01 10:24 by genadmin

Was this page helpful?-10+1

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki