retrieve_sip_conf_from_mysql.pl 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #!/usr/bin/perl -Tw
  2. # Retrieves the sip user/peer entries from the database
  3. # Use these commands to create the appropriate tables in MySQL
  4. #
  5. #CREATE TABLE sip (id INT(11) DEFAULT -1 NOT NULL,keyword VARCHAR(20) NOT NULL,data VARCHAR(50) NOT NULL, flags INT(1) DEFAULT 0 NOT NULL,PRIMARY KEY (id,keyword));
  6. #
  7. # if flags = 1 then the records are not included in the output file
  8. use DBI;
  9. ################### BEGIN OF CONFIGURATION ####################
  10. # the name of the extensions table
  11. $table_name = "sip";
  12. # the path to the extensions.conf file
  13. # WARNING: this file will be substituted by the output of this program
  14. $sip_conf = "/etc/asterisk/sip_additional.conf";
  15. # the name of the box the MySQL database is running on
  16. $hostname = "localhost";
  17. # the name of the database our tables are kept
  18. $database = "sip";
  19. # username to connect to the database
  20. $username = "root";
  21. # password to connect to the database
  22. $password = "";
  23. ################### END OF CONFIGURATION #######################
  24. $additional = "";
  25. open EXTEN, ">$sip_conf" || die "Cannot create/overwrite extensions file: $sip_conf\n";
  26. $dbh = DBI->connect("dbi:mysql:dbname=$database;host=$hostname", "$username", "$password");
  27. $statement = "SELECT keyword,data from $table_name where id=0 and keyword <> 'account' and flags <> 1";
  28. my $result = $dbh->selectall_arrayref($statement);
  29. unless ($result) {
  30. # check for errors after every single database call
  31. print "dbh->selectall_arrayref($statement) failed!\n";
  32. print "DBI::err=[$DBI::err]\n";
  33. print "DBI::errstr=[$DBI::errstr]\n";
  34. exit;
  35. }
  36. my @resultSet = @{$result};
  37. if ( $#resultSet > -1 ) {
  38. foreach $row (@{ $result }) {
  39. my @result = @{ $row };
  40. $additional .= $result[0]."=".$result[1]."\n";
  41. }
  42. }
  43. $statement = "SELECT data,id from $table_name where keyword='account' and flags <> 1 group by data";
  44. $result = $dbh->selectall_arrayref($statement);
  45. unless ($result) {
  46. # check for errors after every single database call
  47. print "dbh->selectall_arrayref($statement) failed!\n";
  48. print "DBI::err=[$DBI::err]\n";
  49. print "DBI::errstr=[$DBI::errstr]\n";
  50. }
  51. @resultSet = @{$result};
  52. if ( $#resultSet == -1 ) {
  53. print "No sip accounts defined in $table_name\n";
  54. exit;
  55. }
  56. foreach my $row ( @{ $result } ) {
  57. my $account = @{ $row }[0];
  58. my $id = @{ $row }[1];
  59. print EXTEN "[$account]\n";
  60. $statement = "SELECT keyword,data from $table_name where id=$id and keyword <> 'account' and flags <> 1 order by keyword";
  61. my $result = $dbh->selectall_arrayref($statement);
  62. unless ($result) {
  63. # check for errors after every single database call
  64. print "dbh->selectall_arrayref($statement) failed!\n";
  65. print "DBI::err=[$DBI::err]\n";
  66. print "DBI::errstr=[$DBI::errstr]\n";
  67. exit;
  68. }
  69. my @resSet = @{$result};
  70. if ( $#resSet == -1 ) {
  71. print "no results\n";
  72. exit;
  73. }
  74. foreach my $row ( @{ $result } ) {
  75. my @result = @{ $row };
  76. print EXTEN "$result[0]=$result[1]\n";
  77. }
  78. print EXTEN "$additional\n";
  79. }
  80. exit 0;