-
Notifications
You must be signed in to change notification settings - Fork 0
/
swvx-cli.py
130 lines (99 loc) · 4.19 KB
/
swvx-cli.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Description: swvx-cli is used to rapidly test switchvox api from command prompt shell.
Change the USERNAME, PASSWORD, and ADDRESS variables to fit your server setup.
Logging by default is set to INFO, change it as needed.
Usage:
./swvx.py <api method> ["parameters"]
e.g.:
./swvx.py switchvox.extensions.search
./swvx.py switchvox.extensions.getInfo "extensions=[899]"
Outputs to the cli are pretty printed.
results are logged by default to the current directory
Analytics:
This script tracks API requests in order to get an ideal of its usage, and what API calls are commonly used.
Collected data is logged to the default log file.
Collected data:
IP Address of where the script is run. (NOT your Swichvox)
Switchvox Method (we do not capture parameters, just the method called.)
How we use this information:
This information is only collected to help us understand how often the script is being used, and what the most
common API calls are that are being made. We hope to use this information in the future to create useful tools.
MIT License
Copyright (c) [year] [fullname]
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
__version__ = '0.0.1'
__author__ = 'VOICE1 LLC <info@voice1-dot-me>'
__copyright__ = '(C) 2017 VOICE1 LLC'
# Change these to match your server!
USERNAME = "admin"
PASSWORD = "admin"
ADDRESS = "SWITCHVOX.IP.ADDRESS"
TIMEOUT = (15, 15)
import sys
import json
import logging
logger = logging.getLogger('swvx-cli')
logging.basicConfig(level=logging.INFO,
filename="swvx-cli.log",
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
try:
from pyswitchvox.client import Client, requests, ExtendAPIError
except ImportError as e:
error_message = "Can not import the pySwitchvox package.\n" \
"Try: pip install pyswitchvox, or visit https://github.com/digium/pyswitchvox"
logger.critical(error_message)
sys.exit(error_message)
try:
logger.debug("Creating switchvox object")
switchvox = Client(address=ADDRESS, username=USERNAME, password=PASSWORD, timeout=TIMEOUT)
except Exception as e:
logger.error("The following error occured: {}".format(e))
sys.exit(1)
if __name__ == '__main__':
logger.debug("argv:", sys.argv)
method = "switchvox.info.getList"
param = ""
result = None
try:
method = sys.argv[1]
except:
logger.warning("No method provided. Using default.")
try:
param = []
for opt in sys.argv[2:]:
param.append(opt)
param = ", ".join(param)
except:
logger.warning("No parameters provided.")
command = "result = %s(%s)" % (method, param)
logger.info("%s(%s)" % (method, param))
# We need to execute the 'string' provided on the CLI as an API call, as the provided string
# maybe arbitary
try:
exec(command)
except ExtendAPIError as e:
logger.critical("API Error: {}".format(e))
sys.exit(1)
except Exception as e:
logger.error("The following error occured: {}".format(e))
data = json.dumps(result, indent=4)
logger.info(data)
print(data)
logger.info("Done.")