Last active
November 4, 2017 20:28
-
-
Save gdugas/5238285 to your computer and use it in GitHub Desktop.
webmin / virtualmin remote api module webmin api: http://doxfer.webmin.com/Webmin/TheWebminAPI virtualmin api: http://www.virtualmin.com/documentation/developer/cli Examples: webmin = WebminApi('localhost', 'mylogin', 'mypassword') print webmin('user', 'list_users') virtualmin = VirtualminApi('localhost', 'mylogin', 'mypassword') print virtualmi…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import base64, httplib, json, xmlrpclib | |
class ApiBase(object): | |
def __init__(self, host, login, password, port=10000, https=True): | |
self.host = host | |
self.login = login | |
self.password = password | |
self.port = port | |
self.https = https | |
class WebminApi(ApiBase): | |
def __init__(self, *args, **kwargs): | |
super(WebminApi, self).__init__(*args, **kwargs) | |
if self.https: | |
url = 'https://' | |
else: | |
url = 'http://' | |
url = url + self.login + ':' + self.password | |
url = url + '@' + self.host + ':' + str(self.port) | |
url = url + '/xmlrpc.cgi' | |
self.proxy = xmlrpclib.ServerProxy(url) | |
def __call__(self, modulename, cmdname, *args, **kwargs): | |
module = getattr(self.proxy, modulename) | |
cmd = getattr(module, cmdname) | |
return cmd(*args, **kwargs) | |
class VirtualminApi(ApiBase): | |
def __init__(self, *args, **kwargs): | |
super(VirtualminApi, self).__init__(*args, **kwargs) | |
if self.https: | |
conn = httplib.HTTPSConnection(self.host, self.port) | |
else: | |
conn = httplib.HTTPConnection(self.host, self.port) | |
self.conn = conn | |
def __call__(self, cmd, *args, **kwargs): | |
return self.send(cmd, *args, **kwargs) | |
def send(self, cmd, *args, **kwargs): | |
url = '/virtual-server/remote.cgi?' | |
params = kwargs | |
for arg in args: | |
params[arg] = '' | |
params['program'] = cmd | |
params['json'] = 1 | |
url = url + '&'.join([x+'='+str(params[x]) for x in params]) | |
headers = {} | |
auth = 'Basic ' + base64.encodestring('%s:%s' % (self.login, | |
self.password)) | |
headers['Authorization'] = auth | |
self.conn.request('GET', url, headers=headers) | |
response = self.conn.getresponse() | |
rawdata = response.read() | |
try: | |
data = json.loads(rawdata) | |
except ValueError: | |
data = rawdata | |
return response.status, response.getheaders, data | |
def close(self): | |
self.conn.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment