freeswitch批量添加用戶


默認情況下,freeswitch內置了1000-1019這20個用戶,如果需要添加更多用戶,可以按如下步驟操作:

一、復制用戶文件

\FreeSWITCH\conf\directory\default 下有1000.xml ~ 1019.xml 這20個用戶的配置文件,以1000.xml為例:

 1 <include>
 2   <user id="1000">
 3     <params>
 4       <param name="password" value="$${default_password}"/>
 5       <param name="vm-password" value="1000"/>
 6     </params>
 7     <variables>
 8       <variable name="toll_allow" value="domestic,international,local"/>
 9       <variable name="accountcode" value="1000"/>
10       <variable name="user_context" value="default"/>
11       <variable name="effective_caller_id_name" value="Extension 1000"/>
12       <variable name="effective_caller_id_number" value="1000"/>
13       <variable name="outbound_caller_id_name" value="$${outbound_caller_name}"/>
14       <variable name="outbound_caller_id_number" value="$${outbound_caller_id}"/>
15       <variable name="callgroup" value="techsupport"/>
16     </variables>
17   </user>
18 </include>

可以寫一段代碼,以1000.xml為模板,批量創建其它用戶的xml:

import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class CreateFreeswitchUser {

    public static void main(String[] args) throws IOException {
        String template = "D:\\soft\\FreeSWITCH\\conf\\directory\\default\\";
        String templateContent = read(template + "1000.xml");
        //創建99個用戶
        for (int i = 1; i < 100; i++) {
            String newUser = "1" + StringUtils.leftPad(i + "", 3, '0');
            String newContent = templateContent.replaceAll("1000", newUser);
            String newFileName = template + newUser + ".xml";
            write(newFileName, newContent);
            System.out.println(newFileName + " done!");
        }
    }

    static String read(String fileName) throws IOException {
        File f = new File(fileName);
        if (!f.exists()) {
            return null;
        }
        FileInputStream fs = new FileInputStream(f);
        String result = null;
        byte[] b = new byte[fs.available()];
        fs.read(b);
        fs.close();
        result = new String(b);
        return result;
    }

    static void write(String fileName, String fileContent) throws IOException {
        File f = new File(fileName);
        if (f.exists()) {
            f.delete();
        }
        FileOutputStream fs = new FileOutputStream(f);
        fs.write(fileContent.getBytes());
        fs.flush();
        fs.close();
    }
}

  

二、調整\FreeSWITCH\conf\dialplan\default.xml

創建用戶的xml后,freeswitch怎么知道加載這些新用戶的xml呢?答案就在dialplan\default.xml這個文件里:

 1 <extension name="Local_Extension">
 2 <!-- 這里可以修改正則范圍,允許所有分機號-->
 3 <condition field="destination_number" expression="^([0-9]\d+)$">
 4 
 5 <!-- ... -->
 6 <action application="set" data="call_timeout=120"/>
 7 <!-- ... -->
 8 
 9 
10 </condition>
11 </extension>

主要是改下正則表達式,允許所有數字。另外,默認還有一個N秒不接認為超時的配置,默認是30秒,如果有需要調整的,也可以一並修改。

 

三、調整\FreeSWITCH\conf\dialplan\public.xml

1 <extension name="public_extensions">
2   <!-- 允許所有分機號 -->
3   <condition field="destination_number" expression="^([0-9]\d+)$">
4       <action application="transfer" data="$1 XML default"/>
5   </condition>
6 </extension>

 

重啟freeswitch,即可生效。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM