-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpanel.py
51 lines (45 loc) · 1.98 KB
/
cpanel.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
import json
import logging
import requests
from constants import *
logger = logging.getLogger()
logger.setLevel(logging.INFO)
def cpanel(target, parameters=None):
'''Make a call to the CPanel UAPI at the given target (module/function) with
parameters and values specified in the parameters dict.
'''
target = target.strip('/')
if not target or '/' not in target:
raise ValueError('Target must be a non-empty string of the form `module/function`')
url = f'https://{CPANEL_SERVER}/execute/{target}'
headers = {'Authorization' : f'cpanel {CPANEL_USERNAME}:{CPANEL_TOKEN}'}
logger.info('GET %s', target)
res = requests.get(url, params=parameters, headers=headers)
if res.status_code != requests.codes.ok:
logger.info('%s', res.text)
raise RuntimeError(f'Request to {url} returned status code {res.status_code}')
logger.info('%s', json.dumps(res.json(), indent=2))
print(json.dumps(res.json(), indent=2))
def cpanel_delete_file(filepath):
'''Deletes the file at filepath from the CPanel server.
NOTE: Uses deprecated CPanel API2 because no delete file function exists in
CPanel UAPI. Ideally, this would be replaced with a standard cpanel() call
once UAPI supports deleting files.
'''
url = f'https://{CPANEL_SERVER}/json-api/cpanel'
headers = {'Authorization' : f'cpanel {CPANEL_USERNAME}:{CPANEL_TOKEN}'}
parameters = {
'cpanel_jsonapi_user': CPANEL_USERNAME,
'cpanel_jsonapi_apiversion': 2,
'cpanel_jsonapi_module': 'Fileman',
'cpanel_jsonapi_func': 'fileop',
'op': 'trash',
'sourcefiles': filepath
}
logger.info('GET Fileman/fileop')
res = requests.get(url, params=parameters, headers=headers)
if res.status_code != requests.codes.ok:
logger.info('%s', res.text)
raise RuntimeError(f'Request to {url} returned status code {res.status_code}')
logger.info('%s', json.dumps(res.json(), indent=2))
print(json.dumps(res.json(), indent=2))