Guest User

Untitled

a guest
Nov 8th, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.49 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. # asterisk.py
  5. #
  6. # Copyright 2014 James Finstrom<jfinstrom at gmail>
  7. #
  8. # This program is free software; you can redistribute it and/or modify
  9. # it under the terms of the GNU General Public License as published by
  10. # the Free Software Foundation; either version 2 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License
  19. # along with this program; if not, write to the Free Software
  20. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  21. # MA 02110-1301, USA.
  22. #
  23. #
  24. import os
  25. import sys
  26. import socket
  27. import ConfigParser
  28.  
  29. mgrcnf = '/etc/asterisk/manager.conf'
  30. mgruser = 'admin'
  31.  
  32. config = ConfigParser.ConfigParser()
  33. config.read(mgrcnf)
  34. username = mgruser
  35. password = config.get( mgrusr, 'secret')
  36. """ Initialize the dictionary in the global space """
  37.  
  38. def make_dict(lst):
  39. ret ={}
  40. for i in lst:
  41. i = i.strip()
  42. if i and i[0] is not "#" and i[-1] is not "=":
  43. var,val = i.rsplit(":",1)
  44. ret[var.strip()] = val.strip()
  45. return ret
  46.  
  47. class acli:
  48. def __init__(self):
  49. self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  50. self.serverip = '127.0.0.1'
  51. self.serverport = 5038
  52. self.username = ''
  53. self.password = ''
  54. def sendCmd(self,action,**args):
  55. self.sock.send("Action: %s\r\n" % action)
  56. for key, value in args.items():
  57. self.sock.send("%s: %s\r\n" % (key,value))
  58. self.sock.send("\r\n")
  59. data = []
  60. while '\r\n\r\n' not in ''.join(data)[-4:]:
  61. buf = self.sock.recv(1)
  62. data.append(buf)
  63. l = ''.join(data).split('\r\n')
  64. return l
  65.  
  66.  
  67. def conn(self):
  68. self.sock.connect((self.serverip, self.serverport))
  69. ret = self.sendCmd("login", Username=self.username, Secret=self.password)
  70. if 'Success' in ret[1]:
  71. return True
  72. else:
  73. return False
  74. def callCalvery(mesg, doing)
  75. #put your action here
  76. pass
  77.  
  78. def main():
  79. ampconf()
  80. ast = acli()
  81. ast.username = username
  82. ast.password = password
  83. if ast.conn():
  84. dev = ast.sendCmd('SIPShowPeer', Peer='1000')
  85. value = make_dict(dev)
  86. if value['Response'] == 'Success':
  87. if value['Status'] == 'OK':
  88. pass
  89. else:
  90. callCalvery(value['status'], 'peer 1000')
  91. else:
  92. callCalvery(value['Message'], 'api call')
  93.  
  94. return 0
  95.  
  96. if __name__ == '__main__':
  97. main()
Add Comment
Please, Sign In to add comment