def addSubnet(self, ip, masc, router=None, nameServer=None): tmp = None try: tmp = Rede(ip, masc) except: return '','' rede = tmp.getRedeAsStr() masc = tmp.getMascAsStr() section = rede+'/'+masc del tmp cfg = self.__getConf() if not cfg.has_section( section ): cfg.add_section( section ) cfg.set( section, 'masc', masc ) if router is None: router = '' cfg.set(section, 'router', router) if nameServer is None: nameServer = '' cfg.set(section, 'nameserver', nameServer) self.__writeConf(cfg) del cfg return section, rede
def getIPMasc(self, iface): from netifaces import ifaddresses from netcontrolrede.netcontrolrede import Rede try: info = ifaddresses(iface)[2][0] info = Rede(info['addr'], info['netmask']) return info.getRedeAsStr() + '/' + info.getMascAsStr() except: return ''
def getIPMasc(self, iface): from netifaces import ifaddresses from netcontrolrede.netcontrolrede import Rede try: info = ifaddresses(iface)[2][0] info = Rede(info['addr'], info['netmask']) return info.getRedeAsStr()+'/'+info.getMascAsStr() except: return ''
def getHostsFrom(self, subnet): from netcontrolrede.netcontrolrede import Rede from netcontroldhcp.netcontroldhcp import DHCP ret = [] subnet = subnet.split('/') for i in DHCP().getHosts(): tsubnet = Rede(i['ip'], i['masc']).getRedeAsStr() tmasc = Rede(i['ip'], i['masc']).getMascAsInt() if tsubnet == subnet[0] and str(tmasc) == str(subnet[1]): ret.append(i) return ret
def delSubnet(self, net, masc): from netcontrolrede.netcontrolrede import MascRede, Rede masc = MascRede(masc).getMascAsStr() try: cfg = self.__getConf() for i in self.getHosts(): tnet = Rede(i['ip'],i['masc']).getRedeAsStr() tmasc = Rede(i['ip'],i['masc']).getMascAsStr() if tnet == net and str(tmasc) == str(masc): cfg.remove_section( i['mac'] ) cfg.remove_section( '%s/%s' % (net,masc) ) self.__writeConf(cfg) del cfg return True except: return False
def addHost(self, name, mac, ip, masc, ltsp=False): from netcontrolrede.netcontrolrede import MascRede, Rede rede = Rede(ip, masc).getRedeAsStr() cfg = self.__getConf() if not cfg.has_section(rede): del cfg cfg = None self.addSubnet(rede, masc) if cfg == None: cfg = self.__getConf() nameCorrect = False newName = name <> '' and name or 'host' if not cfg.has_section(mac): cfg.add_section(mac) else: if cfg.get(mac, 'name') == newName: nameCorrect = True else: nameCorrect = False cfg.set(mac, 'ip', ip) cfg.set(mac, 'masc', MascRede(masc).getMascAsStr()) cfg.set(mac, 'ltsp', ltsp) if not nameCorrect: #tratando o nome name = name.replace(' ','_') newName = name cont = 1 while not self.testValidName(newName): newName = "%s_%d" % (name,cont) cont += 1 cfg.set(mac, 'name', newName) self.__writeConf(cfg) del cfg
def get_ui(self): ui = self.app.inflate('dhcp:main') ui.append('tree', self.get_ui_tree()) #para adicionar os hosts if self._net <> None: hosts = backend.Config(self.app).getHostsFrom(self._net) if hosts == []: ui.append('data', UI.Label(text="Sem máquinas adicionadas")) div = UI.DIV(style='text-valign: center') for i in hosts: ip = i['ip'] masc = MascRede(i['masc']).getMascAsInt() mac = i['mac'] name = i['name'] lenName = len(i['name']) ltsp = i['ltsp'] == 'True' and '1' or '0' div.append( UI.Button(id="editHost/%s/%s/%s/%s/%s" % (name, mac, ip, masc, ltsp), text="Nome: %s\n%s/%s" % (name, ip, masc), action="editHost", design="fixedsize", icon=ltsp == '1' and '/dl/dhcp/terminal.png' or '/dl/dhcp/desktop.png')) ui.append('data', div) if self._mainMessage is not None: ui.find('mainMessage').set('text', self._mainMessage) self._mainMessage = None # SUBNET______________________ if self._editSubnet is None: ui.remove('dlgSubnet') elif self._editSubnet <> []: ui.find('ip').set('value', self._editSubnet[0]) ui.find('mascara').set( 'value', MascRede(self._editSubnet[1]).getMascAsStr()) ui.find('roteador').set('value', self._editSubnet[2]) ui.find('dns').set('value', self._editSubnet[3]) if self._dlgMessage <> None: ui.find('dlgMessageSubnet').set('text', self._dlgMessage) self._dlgMessage = None # END:SUBNET___________________ # HOST____________________ if self._editHost is None: ui.remove('dlgHost') elif self._editHost <> []: ui.find('name').set('value', self._editHost[0]) ui.find('mac').set('value', self._editHost[1]) ui.find('ip').set('value', self._editHost[2]) ui.find('masc').set('value', MascRede(self._editHost[3]).getMascAsStr()) if len(self._editHost) == 5: ui.find(self._editHost[4]).set('selected', 'True') else: ui.remove('delHostOption') if self._dlgMessage <> None: ui.find('dlgMessageHost').set('text', self._dlgMessage) self._dlgMessage = None else: ui.remove('delHostOption') try: if self._net is not None: from netcontrolrede.netcontrolrede import Rede tnet = self._net.split('/') tmp = Rede(tnet[0], tnet[1]) ui.find('ip').set('value', tmp.getRedeAsStr()) ui.find('masc').set('value', tmp.getMascAsStr()) ui.find('formLineIP1').set( 'text', 'Obs.: O IP e a máscara foram preenchidos com') ui.find('formLineIP2').set( 'text', 'a rede escolhida, modifique-o para o novo IP') del tnet del tmp except: pass # END:HOST________________ if self._info is None: ui.remove('dlgInfo') else: from psutil import get_process_list isExecuting = False for i in get_process_list(): if i.name == 'dhcpd': isExecuting = True if isExecuting: ui.find('dlgInfoTitle').set('style', 'text-align: center') ui.find('dlgInfo1').set('text', 'Processo reiniciado com sucesso!') ui.remove('dlgInfo2') ui.remove('divError') else: vc = UI.VContainer() s = False for i in self._info: if i.find('**') <> -1: s = not s continue if not s: vc.append(UI.Label(text=i)) ui.find('divError').append(vc) self._info = None return ui
def get_ui(self): ui = self.app.inflate('dhcp:main') ui.append('tree', self.get_ui_tree()) #para adicionar os hosts if self._net <> None: hosts = backend.Config(self.app).getHostsFrom(self._net) if hosts == []: ui.append('data', UI.Label(text="Sem máquinas adicionadas")) div = UI.DIV(style='text-valign: center') for i in hosts: ip = i['ip'] masc = MascRede(i['masc']).getMascAsInt() mac = i['mac'] name = i['name'] lenName = len(i['name']) ltsp = i['ltsp'] == 'True' and '1' or '0' div.append( UI.Button(id="editHost/%s/%s/%s/%s/%s" % (name, mac, ip, masc, ltsp), text="Nome: %s\n%s/%s" % (name, ip, masc), action="editHost", design="fixedsize", icon=ltsp=='1' and '/dl/dhcp/terminal.png' or '/dl/dhcp/desktop.png' )) ui.append('data', div) if self._mainMessage is not None: ui.find('mainMessage').set('text', self._mainMessage) self._mainMessage = None # SUBNET______________________ if self._editSubnet is None: ui.remove('dlgSubnet') elif self._editSubnet <> []: ui.find('ip').set('value', self._editSubnet[0]) ui.find('mascara').set('value', MascRede(self._editSubnet[1]).getMascAsStr()) ui.find('roteador').set('value', self._editSubnet[2]) ui.find('dns').set('value', self._editSubnet[3]) if self._dlgMessage <> None: ui.find('dlgMessageSubnet').set('text', self._dlgMessage) self._dlgMessage = None # END:SUBNET___________________ # HOST____________________ if self._editHost is None: ui.remove('dlgHost') elif self._editHost <> []: ui.find('name').set('value', self._editHost[0]) ui.find('mac').set('value', self._editHost[1]) ui.find('ip').set('value', self._editHost[2]) ui.find('masc').set('value', MascRede(self._editHost[3]).getMascAsStr()) if len(self._editHost) == 5: ui.find( self._editHost[4] ).set('selected', 'True') else: ui.remove('delHostOption') if self._dlgMessage <> None: ui.find('dlgMessageHost').set('text', self._dlgMessage) self._dlgMessage = None else: ui.remove('delHostOption') try: if self._net is not None: from netcontrolrede.netcontrolrede import Rede tnet = self._net.split('/') tmp = Rede( tnet[0], tnet[1] ) ui.find('ip').set('value', tmp.getRedeAsStr()) ui.find('masc').set('value', tmp.getMascAsStr()) ui.find('formLineIP1').set('text', 'Obs.: O IP e a máscara foram preenchidos com') ui.find('formLineIP2').set('text', 'a rede escolhida, modifique-o para o novo IP') del tnet del tmp except: pass # END:HOST________________ if self._info is None: ui.remove('dlgInfo') else: from psutil import get_process_list isExecuting = False for i in get_process_list(): if i.name == 'dhcpd': isExecuting = True if isExecuting: ui.find('dlgInfoTitle').set('style','text-align: center') ui.find('dlgInfo1').set('text', 'Processo reiniciado com sucesso!') ui.remove('dlgInfo2') ui.remove('divError') else: vc = UI.VContainer() s = False for i in self._info: if i.find('**') <> -1: s = not s continue if not s: vc.append(UI.Label(text=i)) ui.find('divError').append(vc) self._info = None return ui