Example #1
0
def bymac(mac):
	print 'bymac(%s)' % mac
	if type(mac) == types.ListType:
		for i in mac:
			i = str(i)
			print i
			if not validation.is_mac(i):
				raise error.InvalidArgument('Not a valid mac address: %s' % i)
		cond = "mac IN ('%s')" % "','".join(mac)
	else:
		if not validation.is_mac(mac):
			raise error.InvalidArgument('Not a valid mac address: %s' % mac)
		cond = "mac = '%s'" % mac

	data = execute("SELECT ip, mac, COALESCE(NOW() - stopstamp, interval '0 minutes') AS last_seen FROM arpentries JOIN maclastarp ON arpentries.id = maclastarp.arpid WHERE %s;" % cond)
	return mk_dicts(data)
Example #2
0
def bymac(mac):
    print 'bymac(%s)' % mac
    if type(mac) == types.ListType:
        for i in mac:
            i = str(i)
            print i
            if not validation.is_mac(i):
                raise error.InvalidArgument('Not a valid mac address: %s' % i)
        cond = "mac IN ('%s')" % "','".join(mac)
    else:
        if not validation.is_mac(mac):
            raise error.InvalidArgument('Not a valid mac address: %s' % mac)
        cond = "mac = '%s'" % mac

    data = execute(
        "SELECT ip, mac, COALESCE(NOW() - stopstamp, interval '0 minutes') AS last_seen FROM arpentries JOIN maclastarp ON arpentries.id = maclastarp.arpid WHERE %s;"
        % cond)
    return mk_dicts(data)
Example #3
0
	def search(self, q='', expiring=False, page=0, order_by='hostname', success=False, **kw):
		'''
		The search page where the search form POSTs
		'''
		
		# Confirm user authentication
		self.check_session()
		# Initialization
		values = {}
		page = int(page)

		if re.search(r'[^a-zA-Z.,_ ]',order_by):
			raise Exception('Who do you think you are?')
		
		cherrypy.session.acquire_lock()
		try:
			if not q and not kw.keys():
				if not expiring:
					raise cherrypy.InternalRedirect('/hosts')
				else:
					kw['username'] = cherrypy.session['username']
			limit = cherrypy.session['hosts_limit']
		finally:
			cherrypy.session.release_lock()

		if success:
			values['global_success'] = 'Hosts Updated Successfully'
		
		if expiring:
			kw['expiring'] = expiring
		if page:
			kw['page'] = page

		special_search = {
				'ip':'ip', 'mac':'mac', 'user':'******',
				'username':'******', 'net':'network',
				'network':'network', 'hostname':'namesearch',
				'desc':'descriptionsearch','description':'descriptionsearch',
				'name':'namesearch', 'group':'groupname',
				}

		for element in q.split( ):
			if validation.is_mac(element):
				kw['mac'] = element
			elif validation.is_ip(element):
				kw['ip'] = element
			elif validation.is_cidr(element):
				kw['network'] = element
			elif ':' in element:
				# I strongly recommend that we do this next to last...
				stype,value = element.split(':',1)
				if special_search.has_key(stype):
					kw[special_search[stype]] = value
				else:
					raise error.InvalidArgument('Unrecognized special search type: %s (value: %s)' % (stype, value))
				if stype == 'mac' and '*' not in value and len(value) >= 24:
						# range specified
						rawmacs = re.sub(r'[:.-]','',value.strip())
						if not re.match(r"([0-9a-fA-F]{6})[0-9a-fA-F]{6}\1[0-9a-fA-F]{6}", rawmacs):
							raise error.InvalidArgument("Invalid mac range: %s (%s)" % (value, rawmacs))
						kw['mac'] = rawmacs[:12]
						kw['endmac'] = rawmacs[12:]
						
			else:
				# Let's assume it's a hostname.
				if '.' in element or '*' in element or '%' in element:
					namesearch = element.replace('%','*')
				else:
					namesearch = '*%s*' % element.replace('%','*')
				if kw.has_key('namesearch'):
					raise error.InvalidArgument('Invalid search string -- more than one name (%s, %s)' % (kw['namesearch'], namesearch))
				kw['namesearch'] = namesearch


		# FIXME: this might break with special characters
		# FIXME: need more thorough input validation
		kw_elements = []
		kw_keys = kw.keys()
		kw_keys.sort()
		for k in kw_keys:
			v = kw[k]
			if hasattr(v, '__contains__') and '&' in v:
				raise error.InvalidArgument('& is not valid here')
			if k != 'page':
				kw_elements.append('%s=%s' % (k,v))

		search_str = '/search/?%s&' % '&'.join(kw_elements)
		print search_str

		if q:
			# we are ignoring order_by here, but this should only happen with a new search anyway...
			self.redirect('/hosts%s' % ( search_str[:-1] ) )

		kw['order_by'] = order_by

		values['search'] = search_str
		values['page'] = int(page)

		cherrypy.session.acquire_lock()
		try:
			values['show_all_hosts'] = cherrypy.session['show_all_hosts']
			values['username'] = cherrypy.session['username']
		finally:
			cherrypy.session.release_lock()

		values['num_hosts'],values['hosts'] = self.get_hosts( count=True, **kw )
		values['len_hosts'] = len(values['hosts'])
		values['num_pages'] = int( (values['num_hosts'] + limit - 1) / limit )
		values['first_host'] = page * limit + 1
		values['last_host'] = page * limit + len(values['hosts'])
		values['limit'] = limit

		values['order_by'] = order_by
		
		values['url'] = cherrypy.url()

 		values['groups'] = self.webservice.get_groups( { 'ignore_usergroups' : True, 'order_by' : 'name' } )
 		values['dhcp_group_dict'] = {}
		dhcp_groups = self.webservice.get_dhcp_groups( {'order_by' : 'name' } )
		for g in dhcp_groups:
			values['dhcp_group_dict'][g['id']] = dict(g)

		return self.__template.wrap(leftcontent=self.get_leftnav(), filename='%s/templates/hosts.tmpl'%frontend.static_dir, values=values)