#!/usr/bin/env python3 import xmlrpc.client import ssl import sys import os class N4dModules: def __init__(self,server="localhost"): context=ssl._create_unverified_context() self.client = xmlrpc.client.ServerProxy('https://%s:9779'%server,context=context,allow_none=True) self.n4d_key=self.get_n4d_key() if self.n4d_key == None: print("You need administration privileges to run this program.") sys.exit(1) self.actions={} self.actions["load-plugin"]=self.load_plugin self.actions["enable-plugin"]=self.load_plugin self.actions["disable-plugin"]=self.unload_plugin self.actions["unload-plugin"]=self.unload_plugin #def init def get_n4d_key(self): try: f=open("/etc/n4d/key") key=f.readline().strip() f.close() return key except: return None #def get_n4d_key def is_n4d_online(self): try: self.client.get_version() return True except: return False return False #def is_n4d_online def load_plugin(self,plugin_conf_path): if self.is_n4d_online(): sys.stdout.write("Loading %s ...\t"%plugin_conf_path) try: ret=self.client.load_plugin(self.n4d_key,plugin_conf_path) if ret["status"]==0: print("OK") else: print("FAILED. %s"%ret["msg"]) return ret except Exception as e: ret={} ret["status"]=-1 ret["msg"]=str(e) print("FAILED. %s"%str(e)) #def load_plugin def unload_plugin(self,plugin_name): if self.is_n4d_online(): sys.stdout.write("Unloading %s ...\t"%plugin_conf_path) try: ret=self.client.unload_plugin(self.n4d_key,plugin_name) if ret["status"]==0: print("OK") else: print("FAILED. %s"%ret["msg"]) return ret except Exception as e: ret={} ret["status"]=-1 print("FAILED. %s"%str(e)) #def unload_plugin def usage(self): print("USAGE:") print("n4d-modules load-plugin | enable PLUGIN_CONF_FILE") print("n4d-modules unload-plugin | disable-plugin PLUGIN_NAME") #def usage #class N4dModules if __name__=="__main__": n4dm = N4dModules() if len(sys.argv)!=3: n4dm.usage() sys.exit(0) if sys.argv[1] not in n4dm.actions: n4dm.usage() sys.exit(0) if not os.path.exists(sys.argv[2]): print("Plugin configuration file not found [!]") sys.exit(0) if sys.argv[1] in ["load-plugin","enable-plugin"]: ret=n4dm.load_plugin(sys.argv[2]) sys.exit(0) if sys.argv[1] in ["unload-plugin","disable-plugin"]: ret=n4dm.unload_plugin(sys.argv[2]) sys.exit(0)