示例#1
0
	def handle(self, *args, **options):
		if len(args) < 3:
			print "Error: you must supply at least three arguments."
			return
			
		action = args[0].strip().lower()
		mac = ''
		ip = args[2].strip()
		hostname = args[3].strip() if len(args) >= 4 else ''
		
		for c in args[1].strip().lower():
			# remove non-hex characters.
			if c in '0123456789abcdef':
				mac += c
		
		if len(mac) != 12:
			# not the right number of characters.
			print "Error: MAC address must be 6 base16 encoded bytes (12 bytes" + \
				" total)."
			print mac
			return
		
		# TODO: Validate IP address
		
		if action in ('add', 'del'):
			try:
				# see if host exists
				host = NetworkHost.objects.get(mac_address__iexact=mac)
				host.ip_address = ip
				host.online = action == 'add'
				host.computer_name = hostname
				host.save()
			except ObjectDoesNotExist:
				# create new host entry
				host = NetworkHost.objects.create(
					mac_address=mac,
					computer_name=hostname,
					first_connection=utcnow(),
					online=action == 'add',
					ip_address=ip
				)
			# now refresh the owner's hosts, if any.
			if host.user_profile != None:
				sync_user_connections(host.user_profile)
		else:
			print "Error: unknown action %r" % action
			return
示例#2
0
	def handle(self, *args, **options):
		if len(args) < 3:
			print "Error: you must supply at least three arguments."
			return
			
		action = args[0].strip().lower()
		mac = args[1].strip().lower()
		ip = args[2].strip()
		hostname = args[3].strip() if len(args) >= 4 else ''
		if ':' in mac:
			# handle no leading zero on octets split by :
			mac = ''.join([('%02x' % int(x, 16)) for x in mac.split(':')])
		elif '-' in mac:
			# handle no leading zero on octets split by -
			mac = ''.join([('%02x' % int(x, 16)) for x in mac.split('-')])
		
		# last resort, and backup filter to make it probably valid...
		mac2 = ''
		for c in mac:
			# remove non-hex characters.
			if c in string.hexdigits:
				mac2 += c
		mac = mac2
		del mac2
		
		if len(mac) != 12:
			# not the right number of characters.
			print "Error: MAC address must be 6 base16 encoded bytes (12 bytes" + \
				" total)."
			print mac
			return

		# filter hostname (problem with Nintendo 3DS sending hostname with spaces)
		# django has some issue with dealing with spaces in arguments?
		hostname2 = ''
		for c in hostname:
			if (c in string.letters) or (c in string.digits):
				hostname2 += c

		hostname = hostname2
		del hostname2

		# TODO: Validate IP address
		
		if action in ('add', 'del'):
			try:
				# see if host exists
				host = NetworkHost.objects.get(mac_address__iexact=mac)
				host.ip_address = ip
				host.online = action == 'add'
				host.computer_name = hostname
				host.save()
			except ObjectDoesNotExist:
				# create new host entry
				host = NetworkHost.objects.create(
					mac_address=mac,
					computer_name=hostname,
					first_connection=utcnow(),
					online=action == 'add',
					ip_address=ip
				)
			# now refresh the owner's hosts, if any.
			if host.user_profile != None:
				sync_user_connections(host.user_profile)
				
			print "%s host: mac=%s; ip=%s; hostname=%s" % (action, mac, ip, hostname)
		else:
			print "Error: unknown action %r" % action
			return