asterisk_reload_conf.py 1.52 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import socket
import time
import sys

timeout=10
server="asterisk.adm.crans.org"
port=5038

user="django"
password="HLWkzyITZAmBk"

reload_srv={
  'sip':['chan_sip'],
  'voicemail':['app_voicemail'],
  'dialplan':['pbx_config'],
  'all':['chan_sip','app_voicemail','pbx_config'],
}

def reload_config(config):
    if not config in reload_srv.keys():
        print >> sys.stderr, "Reload config from : \n * %s" % ('\n * '.join( reload_srv.keys()))
        return
    sock = socket.socket ( socket.AF_INET, socket.SOCK_STREAM )
    def send(str):
        msg='%s\r\n' % (str)
        sock.send (msg)
    sock.settimeout(timeout)
    sock.connect ( ( server, port ) )
    send ( 'ACTION: LOGIN')
    send ( 'USERNAME: %s' % user)
    send ( 'SECRET: %s' % password)
    send ( '')
    for module in reload_srv[config]:
        send ( 'ACTION: RELOAD')
        send ( 'MODULE: %s' % module)
        send ( 'ActionID: %s' % time.time())
        send ( '')
    send ( 'ACTION: LOGOFF')
    send ( '')
    data=[]
    while True:
        data.extend(sock.recv( 4096 ).split('\r\n'))
        if data==['']:
            sock.close()
            return
        while len(data)>0:
            del data[0]
        
    sock.close()


if __name__ == '__main__' :
    if len(sys.argv)<2:
        print >> sys.stderr, "Usage %s [%s]" %(sys.argv[0],'|'.join(reload_srv.keys()))
        print >> sys.stderr, "Reload config from : \n * %s" % ('\n * '.join(reload_srv.keys()))
        exit(1)
    reload_config(sys.argv[1])