-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcucm_axl.py
150 lines (138 loc) · 4.98 KB
/
cucm_axl.py
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
import configparser
from zeep import Client
from zeep.cache import SqliteCache
from zeep.transports import Transport
from zeep.exceptions import Fault
from zeep.plugins import HistoryPlugin
from requests import Session
from requests.auth import HTTPBasicAuth
from urllib3 import disable_warnings
from urllib3.exceptions import InsecureRequestWarning
from lxml import etree
from datetime import datetime
Timestart = datetime.now()
disable_warnings(InsecureRequestWarning)
Search = []
vars = configparser.ConfigParser()
varfile = 'vars.conf'
vars.read(varfile)
axluser = vars['cucm']['axluser']
axlpassword = vars['cucm']['axlpassword']
cucmhost = vars['cucm']['ip']
path = os.path.dirname(os.path.abspath( __file__ ))
wsdl = os.path.join(path, 'axl', 'AXLAPI.wsdl')
riswsdl = 'https://{}:8443/realtimeservice2/services/RISService70?wsdl'.format(cucmhost)
location = 'https://{host}:8443/axl/'.format(host=cucmhost)
binding = "{http://www.cisco.com/AXLAPIService/}AXLAPIBinding"
def show_history():
for item in [history.last_sent, history.last_received]:
print(etree.tostring(item["envelope"], encoding="unicode", pretty_print=True))
def cucm_rt_phones(model = 255, name = '', num = '', ip = '', max = 1000, Print = True):
StateInfo = ''
if name != '':
SelectBy = 'Name'
SelectItems = {'item': name}
elif num != '':
SelectBy = 'DirNumber'
SelectItems = {'item': num}
elif ip != '':
SelectBy = 'IPV4Address'
SelectItems = {'item': ip}
else:
SelectBy = 'Name'
SelectItems = {}
CmSelectionCriteria = {
'MaxReturnedDevices': max,
'DeviceClass': 'Phone',
'Model': '255',
'Status': 'Registered',
'SelectBy': SelectBy,
'SelectItems': SelectItems
}
session = Session()
session.verify = False
session.auth = HTTPBasicAuth(axluser, axlpassword)
transport = Transport(cache=SqliteCache(), session=session, timeout=5)
history = HistoryPlugin()
client = Client(wsdl=riswsdl, transport=transport, plugins=[history])
def show_history():
for item in [history.last_sent, history.last_received]:
print(etree.tostring(item["envelope"], encoding="unicode", pretty_print=True))
Out = []
i = 0
try:
resp = client.service.selectCmDevice(CmSelectionCriteria=CmSelectionCriteria, StateInfo=StateInfo)
result = resp['SelectCmDeviceResult']['CmNodes']['item']
for node in result:
if node['CmDevices'] != None:
for device in node['CmDevices']['item']:
OutIp = device['IPAddress']['item'][0]['IP']
OutModel = device['Model']
OutDesc = device['Description']
OutNum = device['DirNumber'].replace('-Registered', '')
Out.append({'ip': OutIp, 'model': OutModel, 'desc': OutDesc, 'num': OutNum})
if Print: print(str(list(Out[i].values())))
i += 1
except Fault:
show_history()
return []
return Out
session = Session()
session.verify = False
session.auth = HTTPBasicAuth(axluser, axlpassword)
transport = Transport(cache=SqliteCache(), session=session, timeout=5)
history = HistoryPlugin()
client = Client(wsdl=wsdl, transport=transport, plugins=[history])
service = client.create_service(binding, location)
def getappuser(userid):
try:
resp = service.getAppUser(userid = userid)
result = resp['return']['appUser']
except Fault:
show_history()
result = {}
return result
def appuser_add_device(userid, device):
appuser = getappuser(userid)
devices = appuser['associatedDevices']['device']
if device in devices:
result = 'Unchanged'
else:
devices.append(device)
newdevices = {'device': devices }
try:
service.updateAppUser(userid = userid, associatedDevices = newdevices)
result = 'Success'
except Fault:
show_history()
result = 'Error'
return result
def appuser_remove_device(userid, device):
appuser = getappuser(userid)
devices = appuser['associatedDevices']['device']
if device not in devices:
result = 'Unchanged'
else:
devices.remove(device)
newdevices = {'device': devices }
try:
service.updateAppUser(userid = userid, associatedDevices = newdevices)
result = 'Success'
except Fault:
show_history()
result = 'Error'
return result
def ccm_appuser_reassign(userid, phone, number):
if appuser_remove_device('callrec', phone) == 'Success':
print('Removed {}, number {}'.format(phone, number))
if appuser_add_device('callrec', phone) == 'Success':
print('Added {}, number {}'.format(phone, number))
result = 'Success'
else:
result = 'Error on adding'
else:
result = 'Error on removing'
return result