You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
96 lines
3.0 KiB
96 lines
3.0 KiB
|
|
import sys
|
|
|
|
import logging
|
|
|
|
from twisted.internet import reactor
|
|
from twisted.internet.protocol import ClientFactory
|
|
from twisted.protocols.basic import LineReceiver
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
class AMI():
|
|
def __init__(self,host,port,username,secret,nodenum):
|
|
self.host = host
|
|
self.port = port
|
|
self.username = username.encode('utf-8')
|
|
self.secret = secret.encode('utf-8')
|
|
self.nodenum = str(nodenum).encode('utf-8')
|
|
self.CF = None
|
|
|
|
def send_command(self,command):
|
|
factory = self.AMIClientFactory(
|
|
self.AMIClient,
|
|
self.username,
|
|
self.secret,
|
|
self.nodenum,
|
|
command.encode('utf-8')
|
|
)
|
|
self.CF = reactor.connectTCP(self.host, self.port, factory)
|
|
|
|
def closeConnection(self):
|
|
if self.CF is not None:
|
|
self.CF.disconnect()
|
|
|
|
class AMIClient(LineReceiver):
|
|
|
|
delimiter = b'\r\n'
|
|
|
|
def __init__(self, username, secret, nodenum, command):
|
|
self.username = username
|
|
self.secret = secret
|
|
self.nodenum = nodenum
|
|
self.command = command
|
|
|
|
def connectionMade(self):
|
|
self.sendLine(b'Action: login')
|
|
self.sendLine(b''.join([b'Username: ',self.username]))
|
|
self.sendLine(b''.join([b'Secret: ',self.secret]))
|
|
self.sendLine(self.delimiter)
|
|
|
|
def lineReceived(self,line):
|
|
logger.debug('(AMI) RX: %r', line)
|
|
if line == b'Asterisk Call Manager/1.0':
|
|
return
|
|
|
|
if line == b'Response: Success':
|
|
self.sendLine(b'Action: command')
|
|
self.sendLine(b''.join([b'Command: ',b'rpt cmd ',self.nodenum,b' ',self.command]))
|
|
self.sendLine(self.delimiter)
|
|
self.transport.loseConnection()
|
|
|
|
|
|
|
|
class AMIClientFactory(ClientFactory):
|
|
def __init__(self,AMIClient,username,secret,nodenum,command):
|
|
self.protocol = AMIClient
|
|
self.username = username
|
|
self.secret = secret
|
|
self.nodenum = nodenum
|
|
self.command = command
|
|
|
|
def buildProtocol(self, addr):
|
|
return self.protocol(
|
|
self.username,
|
|
self.secret,
|
|
self.nodenum,
|
|
self.command
|
|
)
|
|
|
|
def clientConnectionFailed(self, connector, reason):
|
|
logger.warning('(AMI) Connection failed: %s', reason)
|
|
ClientFactory.clientConnectionFailed(self, connector, reason)
|
|
|
|
def clientConnectionLost(self, connector, reason):
|
|
logger.debug('(AMI) Connection lost: %s', reason)
|
|
ClientFactory.clientConnectionLost(self, connector, reason)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
|
a = AMI(sys.argv[1],int(sys.argv[2]),'admin','llcgi',29177)
|
|
#AMIOBJ.AMIClientFactory(AMIOBJ.AMIClient,'rpt cmd 29177 ilink 3 2001')
|
|
a.send_command(sys.argv[3])
|
|
reactor.run()
|