import subprocess import os import shutil import tempfile class RegisterMachine: def __init__(self): #self.pathfile = "/var/log/n4d/macs_id" self.pathfile = "/var/lib/llxcfg-dnsmasq/macs/all-hosts" self.dnsfile = "/var/lib/llxcfg-dnsmasq/hosts/hosts-reg" self.teacherfile = "/var/log/n4d/teachermachine" self.locktocken = "/tmp/lockmacregister" if not os.path.exists(self.pathfile): open(self.pathfile,'a') prevmask = os.umask(0) os.chmod(self.pathfile,0644) os.lchown(self.pathfile,0,0) os.umask(prevmask) if not os.path.exists(self.teacherfile): open(self.teacherfile,'a') prevmask = os.umask(0) os.chmod(self.teacherfile,0774) os.lchown(self.teacherfile,0,0) os.umask(prevmask) #def __init__ def get_available_id_list(self): pprocess = subprocess.Popen(['llxcfg-showvars','DHCP_HOST_MAX'],stdout = subprocess.PIPE,stderr = subprocess.PIPE) try: maxlimit = int(pproces.stdout.readline().split("'")[1]) maxlimit += 1 except: maxlimit = 81 lavailable = range(1,maxlimit) return {'status':True,'result':lavailable} #def get_registred_machines def remove_register(self,mac): if not os.path.exists(self.locktocken): open(self.locktocken,'a') else: return {'status':False,'result':'Server is locked now' } registerfile = open(self.pathfile,'r') content = registerfile.readlines() registerfile.close() found = False new_content = [] #dhcp-host=00:0F:FE:C3:D9:EC,10.0.2.1,llx-pc01 for line in content: try: auxline = line.strip('\n') auxline = auxline.split('=')[1] lmac,lip,lhostname = auxline.split(',') except: continue if lmac == mac : found = True else: new_content.append(line) if not found : os.remove(self.locktocken) return {'status':False,'result':'MAC ' + mac + ' is not registred' } else: #mac founded registerfile = open(self.pathfile,'w') for line in new_content: registerfile.write(line) registerfile.close() macfile = open(self.teacherfile,'r') maccontent = macfile.readlines() macfile.close() new_maccontent = [] for lmac in maccontent: lmac = lmac.strip('\n') if lmac != idtoremove : new_maccontent.append(lmac) macfile = open(self.teacherfile,'w') for line in new_maccontent: macfile.write(line+'\n') macfile.close() os.remove(self.locktocken) return {'status':True,'result':'MAC '+mac+' has been removed' } #def register_machine def register_machine(self,id,mac,isteacher): if not os.path.exists(self.locktocken): open(self.locktocken,'a') else: return {'status':False,'result':'Server is locked now' } teacherfile = open(self.teacherfile,'a') registerfile = open(self.pathfile,'r') dnsfile = open(self.dnsfile,'r') content = registerfile.readlines() new_content = [] found = False pprocess = subprocess.Popen(['llxcfg-showvars','HOSTNAME_PREFIX'],stdout = subprocess.PIPE,stderr = subprocess.PIPE) try: new_hostname_prefix = pproces.stdout.readline().split("'")[1] except: new_hostname_prefix = 'llx-pc' if int(id) < 10 : new_hostname = new_hostname_prefix + "0" + id else: new_hostname = new_hostname_prefix + id for line in content: try: auxline = line.split('=')[1] lmac,lip,lhostname = auxline.split(',') lhostname = lhostname.strip() except: continue if new_hostname != lhostname and lmac != mac: new_content.append(line) all_lines = dnsfile.readlines() new_ip = '' for a in all_lines: if new_hostname in a: new_ip = a.split(" ")[0] break if new_ip == '': os.remove(self.locktocken) return {'status':False,'result':'Not found ip for ' + new_hostname} new_content.append("dhcp-host=" + mac + "," + new_ip + ","+ new_hostname +"\n") if isteacher: teacherfile.write(str(id)+"\n") registerfile.close() registerfile = open(self.pathfile,'w') for line in new_content: registerfile.write(line) registerfile.close() os.remove(self.locktocken) return {'status':True,'result':'MAC '+ mac + ' has been registered with id ' + id } #def register_machine