脚本说明

自动创建用户,并保存到direcory目录中。

创建单用户脚本

文件:create_user.lua
调用方式: create_user.lua 1000 password

  1. function log(txt, level)
  2. level = level or "debug"
  3. freeswitch.consoleLog(level, argv[0] .. ":" .. txt .. "\n");
  4. end
  5. function generatenum(uspath,num,pwd)
  6. local fullFilePath = uspath .. "/" .. num .. ".xml";
  7. --if file_exists(fullFilePath) then
  8. --log("directory file:" .. fullFilePath .. " already exist, skip");
  9. --return;
  10. --end
  11. local wc=io.open(fullFilePath,"w");
  12. wc:write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
  13. wc:write("<include>\n");
  14. wc:write(" <user id=\"" .. num .. "\" mailbox=\"" .. num .. "\">\n");
  15. wc:write(" <params>\n");
  16. wc:write(" <param name=\"password\" value=\"" .. pwd .. "\"/>\n");
  17. wc:write(" <param name=\"vm-password\" value=\"" .. pwd .. "\"/>\n");
  18. wc:write(" </params>\n");
  19. wc:write(" <variables>\n");
  20. wc:write(" <variable name=\"toll_allow\" value=\"domestic,international,local\"/>\n");
  21. wc:write(" <variable name=\"accountcode\" value=\"" .. num .. "\"/>\n");
  22. wc:write(" <variable name=\"user_context\" value=\"default\"/>\n");
  23. wc:write(" <variable name=\"effective_caller_id_name\" value=\"Extension " .. num .. "\"/>\n");
  24. wc:write(" <variable name=\"effective_caller_id_number\" value=\"" .. num .. "\"/>\n");
  25. wc:write(" <variable name=\"outbound_caller_id_name\" value=\"$${outbound_caller_name}\"/>\n");
  26. wc:write(" <variable name=\"outbound_caller_id_number\" value=\"$${outbound_caller_id}\"/>\n");
  27. wc:write(" <variable name=\"callgroup\" value=\"" .. num .. "\"/>\n");
  28. wc:write(" </variables>\n");
  29. wc:write(" </user>\n");
  30. wc:write("</include>\n");
  31. wc:close();
  32. end
  33. num = argv[1];
  34. pwd = argv[2];
  35. api = freeswitch.API()
  36. confdir = api:executeString("global_getvar conf_dir");
  37. local fsPath = confdir .."/directory/default";
  38. generatenum(fsPath,num, pwd);
  39. api:executeString("reloadxml");
  40. log("create number end")

创建多用户脚本

文件:number_generator.lua
调用方式:number_generator.lua 1000 1030
脚本说明:自动创建1000到1030号码,密码与号码相同

  1. function log(txt, level)
  2. level = level or "debug"
  3. freeswitch.consoleLog(level, argv[0] .. ":" .. txt .. "\n");
  4. end
  5. function file_exists(name)
  6. local f=io.open(name,"r")
  7. if f~=nil then io.close(f) return true else return false end
  8. end
  9. function generatenum(uspath,num)
  10. local fullFilePath = uspath .. "/" .. num .. ".xml";
  11. if file_exists(fullFilePath) then
  12. log("directory file:" .. fullFilePath .. " already exist, skip");
  13. return;
  14. end
  15. local wc=io.open(fullFilePath,"w");
  16. wc:write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\n");
  17. wc:write("<include>\n");
  18. wc:write(" <user id=\"" .. num .. "\" mailbox=\"" .. num .. "\">\n");
  19. wc:write(" <params>\n");
  20. wc:write(" <param name=\"password\" value=\"" .. num .. "\"/>\n");
  21. wc:write(" <param name=\"vm-password\" value=\"" .. num .. "\"/>\n");
  22. wc:write(" </params>\n");
  23. wc:write(" <variables>\n");
  24. wc:write(" <variable name=\"toll_allow\" value=\"domestic,international,local\"/>\n");
  25. wc:write(" <variable name=\"accountcode\" value=\"" .. num .. "\"/>\n");
  26. wc:write(" <variable name=\"user_context\" value=\"default\"/>\n");
  27. wc:write(" <variable name=\"effective_caller_id_name\" value=\"Extension " .. num .. "\"/>\n");
  28. wc:write(" <variable name=\"effective_caller_id_number\" value=\"" .. num .. "\"/>\n");
  29. wc:write(" <variable name=\"outbound_caller_id_name\" value=\"$${outbound_caller_name}\"/>\n");
  30. wc:write(" <variable name=\"outbound_caller_id_number\" value=\"$${outbound_caller_id}\"/>\n");
  31. wc:write(" <variable name=\"callgroup\" value=\"" .. num .. "\"/>\n");
  32. wc:write(" </variables>\n");
  33. wc:write(" </user>\n");
  34. wc:write("</include>\n");
  35. wc:close();
  36. end
  37. function trim(s)
  38. return (string.gsub(s, "^%s*(.-)%s*$", "%1"))
  39. end
  40. function create(startIndex, endIndex, filePath)
  41. for i=startIndex,endIndex do
  42. local str = tostring(i)
  43. if str:sub(-2) == ".0" then
  44. str = str:sub(1,-3)
  45. end
  46. generatenum(filePath, str)
  47. end
  48. end
  49. startIndex = argv[1];
  50. endIndex = argv[2];
  51. api = freeswitch.API()
  52. confdir = api:executeString("global_getvar conf_dir");
  53. local fsPath = confdir .."/directory/default";
  54. create(startIndex, endIndex, fsPath);
  55. api:executeString("reloadxml");
  56. log("create number end")