When trying to concatenate two strings, they combine but the next three characters overwrite earlier concatenated text before again continuing to concatenate as expected. I suspect this is something to do with the retrieve_mapped_value(cmd.Interaction.Replies[p_index].Variable[r_index], var_swap)
function as this phenomena only happens when it is called within the double for
of the cmd.Interaction
section of build_executable
.
Where the error is
func build_executable(cmd shell_command, var_swap string_matrix, is_first bool) string{
sleep_duration := cmd.Sleep
result := ""
if !is_first{
result = "send \""
} else {
result = "spawn "
}
result += cmd.Command
if len(cmd.Options.Dashes) > 0 {
for index := range cmd.Options.Dashes{
if cmd.Options.Dashes[index] != "" || cmd.Options.Flags[index] != ""{
result += " " + cmd.Options.Dashes[index] + cmd.Options.Flags[index]
}
if cmd.Options.Values[index] != ""{
result += " " + cmd.Options.Values[index]
}
}
}
if len(cmd.Targets.Litteral) > 0{
result += " "
for index := range cmd.Targets.Litteral{
result += cmd.Targets.Litteral[index] + retrieve_mapped_value(cmd.Targets.Variable[index], var_swap)
}
}
if !is_first{
result += "\\"
"
} else {
result += "
"
}
result += "sleep " + sleep_duration + "
"
if len(cmd.Interaction.Prompts) > 0{
for p_index := range cmd.Interaction.Prompts{
fmt.Println("cmd.Interaction.Prompts[p_index]\t" + cmd.Interaction.Prompts[p_index])
result += "expect \"" + cmd.Interaction.Prompts[p_index] + "\"
send \""
for r_index := range cmd.Interaction.Replies[p_index].Litteral{
fmt.Println("cmd.Interaction.Replies[p_index].Litteral[r_index]\t'" + cmd.Interaction.Replies[p_index].Litteral[r_index] + "'")
fmt.Println("cmd.Interaction.Replies[p_index].Variable[r_index]\t'" + cmd.Interaction.Replies[p_index].Variable[r_index] + "'")
fmt.Println("retrieve_mapped_value(cmd.Interaction.Replies[p_index].Variable[r_index], var_swap)\t'" + retrieve_mapped_value(cmd.Interaction.Replies[p_index].Variable[r_index], var_swap) + "'")
result += cmd.Interaction.Replies[p_index].Litteral[r_index]
result += retrieve_mapped_value(cmd.Interaction.Replies[p_index].Variable[r_index], var_swap)
result += "" + "" + ""
}
result += "\\"
sleep " + sleep_duration + "
"
}
}
if cmd.Expects != "" {
result += "expect \"" + cmd.Expects + "\"
"
}
return result
}
Suspect function
func retrieve_mapped_value(key string, mapping string_matrix) string{
if key != "" {
for _, layer := range mapping{
if layer[0] == key {
return layer[1]
}
}
} else {
return key
}
return "***No Match Error***"
}
What I should get
expect "Enter password for user root: "
send "e3H-*HGHu__7"
sleep 10
What I actually get
expect "Enter password for user root: "
"d "e3H-*HGHu__7
sleep 10
It is taking the last 3 characters of one line and overwriting the front with them. I don't understand.