asterisk - Simplifying device creation in sip.conf -
i have define many similar devices in sip.conf this:
[device](!) ; setting parameters [device01](device) callerid=dev01 <01> [device02](device) callerid=dev02 <02> ; ... [devicexx](device) callerid=devxx <xx>
the question perhaps avoid setting device-name specific parameters using variable following?
[device](!) callerid=dev${device_name:-2} <${device_name:-2}> ; setting parameters [device01](device) [device02](device) ; ... [devicexx](device)
p.s. perfect, if there device constructor, reduce script following, but, think, not possible in asterisk.
[device](!) callerid=dev${device_name:-2} <${device_name:-2}> ; setting parameters ;[device${magic_loop(1,xx,leading_zeroes)}](device)
i've had results writing small program takes care of it. checks line saying like
------- automatically generated -------
and whatever after line, it's going regenerated detects there new values (it database or text file). then, run supervisor , checks every xx seconds if there changes.
if there changes, issues sip reload
command after updating sip.conf file
i wrote in python, whatever language feel comfortable should work fine.
that's how managed , has been working fine far (after couple of months). i'd extremely interested in learning other approaches though. it's (called script supervisor):
users = get_users_logic() #get data me used on sip.conf file data_to_be_hashed = reduce(lambda x, y: x + y, map(lambda x: x['username'] + x['password'] + x['company_prefix'], users)) m = hashlib.md5() m.update(str(data_to_be_hashed).encode("ascii")) new_md5 = m.hexdigest() last_md5 = none try: file = open(os.path.dirname(os.path.realpath(__file__)) + '/lastmd5.txt', 'r') last_md5 = file.read().rstrip() file.close() except: pass # if changed... if new_md5 != last_md5: #needs update open(settings['asterisk']['path_to_sip_conf'], 'r') file: sip_content = file.read().rstrip() parts = sip_content.split(";-------------- beyond point auto generated --------------;") sip_content = parts[0].rstrip() sip_content += "\n\n;-------------- beyond point auto generated --------------;\n\n" user in users: m = hashlib.md5() m.update(("%s:sip.ellauri.it:%s" % (user['username'], user['password'])).encode("ascii")) md5secret = m.hexdigest() sip_content += "[%s]\ntype = friend\ncontext = %slocal\nmd5secret = %s\nhost = dynamic\n\n" % ( user['username'], user['company_prefix'], md5secret) #write sip.conf file f = open(settings['asterisk']['path_to_sip_conf'], 'w') print(sip_content, file=f) f.close() subprocess.call('asterisk -x "sip reload"', shell=true) #write new md5 f = open(os.path.dirname(os.path.realpath(__file__)) + '/lastmd5.txt', 'w') print(new_md5, file=f) f.close()
Comments
Post a Comment