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