func (c *Client) Import(host, ip string) (*Agent, error) {
	importFile := filepath.Join(c.tempDir, fmt.Sprintf("ossec_import.%s.lst", host))

	if host == "" {
		return nil, fmt.Errorf("no host specified")
	}
	if ip == "" {
		ip = "any"
	}

	agent, err := c.Find("name", host)
	if err != nil {
		return nil, err
	}

	// if the agent was found, throw error
	if agent != nil {
		return agent, fmt.Errorf("agent with Name: %s was found with ID: %s, IP: %s", agent.Name, agent.ID, agent.IP)
	}

	sudo := "sudo"
	if c.noSudo {
		sudo = ""
	}

	entryCmd := strings.TrimSpace(
		fmt.Sprintf(
			"%s echo \"%s,%s\" >%s",
			sudo,
			ip,
			host,
			importFile,
		),
	)

	importCmd := strings.TrimSpace(
		fmt.Sprintf(
			"%s %s -f %s",
			sudo,
			c.manageExec,
			importFile,
		),
	)

	removeCmd := strings.TrimSpace(
		fmt.Sprintf(
			"%s rm -f %s",
			sudo,
			importFile,
		),
	)

	// perform import
	_, err = c.run(
		fmt.Sprintf("%s && %s && %s", entryCmd, importCmd, removeCmd),
	)
	if err != nil {
		return nil, fmt.Errorf("failed to import the host: %v", err)
	}

	newAgent, err := c.Find("name", host)
	if err != nil {
		return nil, err
	} else if newAgent == nil {
		return nil, fmt.Errorf("import unsuccessful, host %s not found", host)
	}

	return newAgent, nil
}
// Import imports a host and returns its id and key
Import imports a host and returns its id and key
// Import imports a host and returns its id and key
func (c *Client) Import(host, ip string) (*Agent, error) {
	importFile := filepath.Join(c.tempDir, fmt.Sprintf("ossec_import.%s.lst", host))
	if host == "" {
		return nil, fmt.Errorf("no host specified")
	}
	if ip == "" {
		ip = "any"
	}
	agent, err := c.Find("name", host)
	if err != nil {
		return nil, err
	}
	if agent != nil {
		return agent, fmt.Errorf("agent with Name: %s was found with ID: %s, IP: %s", agent.Name, agent.ID, agent.IP)
	}
	sudo := "sudo"
	if c.noSudo {
		sudo = ""
	}
	entryCmd := strings.TrimSpace(
		fmt.Sprintf(
			"%s echo \"%s,%s\" >%s",
			sudo,
			ip,
			host,
			importFile,
		),
	)
	importCmd := strings.TrimSpace(
		fmt.Sprintf(
			"%s %s -f %s",
			sudo,
			c.manageExec,
			importFile,
		),
	)
	removeCmd := strings.TrimSpace(
		fmt.Sprintf(
			"%s rm -f %s",
			sudo,
			importFile,
		),
	)
	_, err = c.run(
		fmt.Sprintf("%s && %s && %s", entryCmd, importCmd, removeCmd),
	)
	if err != nil {
		return nil, fmt.Errorf("failed to import the host: %v", err)
	}
	newAgent, err := c.Find("name", host)
	if err != nil {
		return nil, err
	} else if newAgent == nil {
		return nil, fmt.Errorf("import unsuccessful, host %s not found", host)
	}
	return newAgent, nil
}
func ( c * Client ) Import ( host , ip string ) ( * Agent , error ) { importFile := filepath . Join ( c . tempDir , fmt . Sprintf ( " " , host ) ) if host == " " { return nil , fmt . Errorf ( " " ) } if ip == " " { ip = " " } agent , err := c . Find ( " " , host ) if err != nil { return nil , err } if agent != nil { return agent , fmt . Errorf ( " " , agent . Name , agent . ID , agent . IP ) } sudo := " " if c . noSudo { sudo = " " } entryCmd := strings . TrimSpace ( fmt . Sprintf ( " \" \" " , sudo , ip , host , importFile , ) , ) importCmd := strings . TrimSpace ( fmt . Sprintf ( " " , sudo , c . manageExec , importFile , ) , ) removeCmd := strings . TrimSpace ( fmt . Sprintf ( " " , sudo , importFile , ) , ) _ , err = c . run ( fmt . Sprintf ( " " , entryCmd , importCmd , removeCmd ) , ) if err != nil { return nil , fmt . Errorf ( " " , err ) } newAgent , err := c . Find ( " " , host ) if err != nil { return nil , err } else if newAgent == nil { return nil , fmt . Errorf ( " " , host ) } return newAgent , nil }
Import imports a host and returns its id and key
Import imports a host and returns its id and key
// if the agent was found, throw error // perform import