sip_to_pjsql.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #!/usr/bin/env python
  2. from sip_to_pjsip import convert
  3. import sip_to_pjsip
  4. import optparse
  5. import sqlconfigparser
  6. def write_pjsip(filename, pjsip, non_mappings):
  7. """
  8. Write pjsip.sql file to disk
  9. """
  10. try:
  11. with open(filename, 'wt') as fp:
  12. pjsip.write(fp)
  13. except IOError:
  14. print("Could not open file " + filename + " for writing")
  15. def cli_options():
  16. """
  17. Parse command line options and apply them. If invalid input is given,
  18. print usage information
  19. """
  20. global user
  21. global password
  22. global host
  23. global port
  24. global database
  25. global table
  26. usage = "usage: %prog [options] [input-file [output-file]]\n\n" \
  27. "Converts the chan_sip configuration input-file to mysql output-file.\n" \
  28. "The input-file defaults to 'sip.conf'.\n" \
  29. "The output-file defaults to 'pjsip.sql'."
  30. parser = optparse.OptionParser(usage=usage)
  31. parser.add_option('-u', '--user', dest='user', default="root",
  32. help='mysql username')
  33. parser.add_option('-p', '--password', dest='password', default="root",
  34. help='mysql password')
  35. parser.add_option('-H', '--host', dest='host', default="127.0.0.1",
  36. help='mysql host ip')
  37. parser.add_option('-P', '--port', dest='port', default="3306",
  38. help='mysql port number')
  39. parser.add_option('-D', '--database', dest='database', default="asterisk",
  40. help='mysql port number')
  41. parser.add_option('-t', '--table', dest='table', default="sippeers",
  42. help='name of sip realtime peers table')
  43. options, args = parser.parse_args()
  44. user = options.user
  45. password = options.password
  46. host = options.host
  47. port = options.port
  48. database = options.database
  49. table = options.table
  50. sip_filename = args[0] if len(args) else 'sip.conf'
  51. pjsip_filename = args[1] if len(args) == 2 else 'pjsip.sql'
  52. return sip_filename, pjsip_filename
  53. if __name__ == "__main__":
  54. sip_filename, pjsip_filename = cli_options()
  55. sip = sqlconfigparser.SqlConfigParser(table)
  56. sip_to_pjsip.sip = sip
  57. sip.connect(user,password,host,port,database)
  58. print('Please, report any issue at:')
  59. print(' https://issues.asterisk.org/')
  60. print('Reading ' + sip_filename)
  61. sip.read(sip_filename)
  62. print('Converting to PJSIP realtime sql...')
  63. pjsip, non_mappings = convert(sip, pjsip_filename, dict(), False)
  64. print('Writing ' + pjsip_filename)
  65. write_pjsip(pjsip_filename, pjsip, non_mappings)