예제 #1
0
def executeTask(taskpath, device, uargs, user):
	"""Executes a (number of) command(s) to a certain device and obtains parsed cli output
	
	Keyword arguments:
	taskpath -- Used to find a specific task in the task dictionary for this device
	device   -- The device to send the commands to
	uargs    -- The user-supplied arguments
	user     -- The NMS user making this request
	
	Return value:
	List of strings
	"""
	xmlroot = xmlparser.get_xml_struct(device.gen_dev_id.file_location_id.location)
	taskpath = taskpath.split('|')
	commands = xmlparser.getAvailableTasks(xmlroot)
	for key in taskpath:
		if not key in commands.keys():
			return ['Invalid command']
		commands = commands[key]
	uarg_names, raw_args, parser = commands
	args = []
	for raw_arg in raw_args:
		args.append(raw_arg)
	for uarg_name in uarg_names:
		if not uarg_name in uargs.keys():
			return ['User arguments not supplied']
	for i in range(len(args)):
		for uarg_key in uargs.keys():
			if type(args[i]) == type(str()):
				args[i] = args[i].replace('%arg:' + uarg_key + '%', uargs[uarg_key])

	if device.pref_remote_prot == 'SSH2':
		s = sshconnection.SSHConnection(device.ip, device.login_name, passwordstore.getRemotePassword(device), device.port)
	elif device.pref_remote_prot == 'Telnet':
		s = telnetconnection.TelnetConnection(device.ip, device.login_name, passwordstore.getRemotePassword(device), device.port)
	else:
		return ['Unknown protocol defined for device']
	if s.connect() == -1:
		return ['Unable to connect']
	ret = b''
	for i, arg in enumerate(args):
		if type(arg) != type(bytes()):
			History.objects.create(user_performed_task = user, action_type='Manage device', dev_id = device, action = '[dev%i] %s' % (device.dev_id, arg), date_time = timezone.now())
		if i+1 == len(args):
			ret += s.send_and_receive(arg, delay=4)
		else:
			ret += s.send_and_receive(arg)
	s.close()
	return parser.parse(ret)
예제 #2
0
def getInterfaces(command, parser, device, user):
	"""Obtains a list of all interfaces available on the device
	
	Keyword arguments:
	command -- The command used to obtain the interface names from the device
	parser  -- A regex parser to create a list of interface names from the devices console output
	device  -- The device to obtain the interface names for
	user    -- The NMS user making this request
	
	Return value:
	List of strings	or None
	"""
	global interfacesLock
	global interfaces
	interfacesLock.acquire()
	try:
		if not device in interfaces.keys():
			if device.pref_remote_prot == 'SSH2':
				s = sshconnection.SSHConnection(device.ip, device.login_name, passwordstore.getRemotePassword(device), device.port)
			elif device.pref_remote_prot == 'Telnet':
				s = telnetconnection.TelnetConnection(device.ip, device.login_name, passwordstore.getRemotePassword(device), device.port)
			else:
				return None
			if s.connect() == -1:
				return None
			ret = b''
			for i, arg in enumerate(command):
				if type(arg) != type(bytes()):
					History.objects.create(user_performed_task = user, action_type='Manage device', dev_id = device, action = '[dev%i] %s' % (device.dev_id, arg), date_time = timezone.now())
				if i+1 == len(command):
					ret += s.send_and_receive(arg, delay=4)
				else:
					ret += s.send_and_receive(arg)
			s.close()
			interfaces[device] = parser.parse(ret)
		return interfaces[device]
	except:
		raise
	finally:
		interfacesLock.release()
예제 #3
0
def __createTelnetConnection__(device):
	"""Creates a Telnet connection used in interactive sessions
	
	Keyword arguments:
	device -- The device to connect to
	
	Return value:
	nms.telnetconnection.TelnetConnection object	
	"""
	connection = telnetconnection.TelnetConnection(device.ip, device.login_name, passwordstore.getRemotePassword(device), device.port)
	connection.connect()
	connection.get_socket().setblocking(0)
	return connection
예제 #4
0
def __createSSHConnection__(device):
	"""Creates a SSH connection used in interactive sessions
	
	Keyword arguments:
	device -- The device to connect to
	
	Return value:
	nms.sshconnection.SSHConnection object	
	"""
	connection = sshconnection.SSHConnection(device.ip, device.login_name, passwordstore.getRemotePassword(device), device.port)
	connection.connect()
	connection.chan.setblocking(0)
	return connection