import sys import os import xmlrpclib class Shutdowner: tcos_port="9779" clients_file="/run/clients" def __init__(self): pass #def init def list_clients(self): try: # save_path, name_file, hdd_disk, final_action environment_variables = {} # Get the values from free server if objects.has_key("VariablesManager"): environment_variables=objects["VariablesManager"].get_variable_list(["SRV_IP","INTERNAL_NETWORK","INTERNAL_MASK"]) SRV_IP=environment_variables["SRV_IP"] INTERNAL_NETWORK=environment_variables["INTERNAL_NETWORK"] INTERNAL_MASK=environment_variables["INTERNAL_MASK"] os.system('nmap -sS -p%s -oG %s %s' % (self.tcos_port,self.clients_file,str(INTERNAL_NETWORK)+'/'+str(INTERNAL_MASK))) os.system("cat %s |grep open|egrep -o '[0-9]+([.][0-9]+){3}'|uniq|grep -v %s > %s" %(self.clients_file,str(SRV_IP),self.clients_file+'_tmp')) os.system ('rm %s' %(self.clients_file)) COMMENT_END = "Clients in your internal network has been saved in: %s" %(self.clients_file+'_tmp') return [True,str(COMMENT_END)] except Exception as e: return [False,str(e)] #def list_clients def shutdownlist(self,user,password,aux_clients_file=clients_file+'_tmp'): try: with open(aux_clients_file) as f: for line in f: c=xmlrpclib.ServerProxy("https://%s:9779"%line.strip("\n")) u=(user,password) try: c.shutdown(u,"ShutdownerClient") except Exception as e: print e,line.strip("\n") COMMENT_END = "The PC's in your internal network have been halted, you can review their address in: %s" %(aux_clients_file) return [True,str(COMMENT_END)] except Exception as e: return [False,str(e)] #def shutdownlist def shutdowncronlist(self,user,password,min,hour,monthday,month,weekday,aux_clients_file=clients_file+'_tmp'): try: with open(aux_clients_file) as f: for line in f: c=xmlrpclib.ServerProxy("https://%s:9779"%line.strip("\n")) u=(user,password) try: c.shutdown_cron(u,"ShutdownerClient",min,hour,monthday,month,weekday) except Exception as e: print e,line.strip("\n") COMMENT_END = "The PC's in your internal network have been updated with new cron to shutdown at hour: %s, min: %s, weekday: %s, days of the month: %s and months: %s" %(hour,min,weekday,monthday,month) return [True,str(COMMENT_END)] except Exception as e: return [False,str(e)] #def shutdowncronlist def noshutdowncronlist(self,user,password,aux_clients_file=clients_file+'_tmp'): try: with open(aux_clients_file) as f: for line in f: c=xmlrpclib.ServerProxy("https://%s:9779"%line.strip("\n")) u=(user,password) try: c.noshutdown_cron(u,"ShutdownerClient") except Exception as e: print e,line.strip("\n") COMMENT_END = "The PC's in your internal network have been updated, cron deactivated!" return [True,str(COMMENT_END)] except Exception as e: return [False,str(e)] #def noshutdowncronlist