当尝试连接两个字符串时,它们会合并,但接下来的三个字符会覆盖之前连接的文本,然后再次按预期继续连接。我怀疑这与 retrieve_mapped_value(cmd.Interaction.Replies[p_index].Variable[r_index], var_swap)
函数有关,因为这种现象仅发生在在 build_executable
的 cmd.Interaction
部分的双 for
中调用。
哪里出错了
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 += "\\r\"\n"
} else {
result += "\n"
}
result += "sleep " + sleep_duration + "\n"
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] + "\"\nsend \""
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 += "\\r\"\nsleep " + sleep_duration + "\n"
}
}
if cmd.Expects != "" {
result += "expect \"" + cmd.Expects + "\"\n"
}
return result
}
可疑函数
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***"
}
我应该得到什么
expect "Enter password for user root: "
send "e3H-*HGHu__7\r"
sleep 10
我实际得到的
expect "Enter password for user root: "
\r"d "e3H-*HGHu__7
sleep 10
它取一行的最后 3 个字符并用它们覆盖前面。我不明白。
最佳答案
我从来没有找到解决这个问题的方法,但是下面的 temp_add
部分我任意切掉了字符串的末尾。这里有一些巫毒教。
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 += "\\r\"\n"
} else {
result += "\n"
}
result += "sleep " + sleep_duration + "\n"
if len(cmd.Interaction.Prompts) > 0{
for p_index := range cmd.Interaction.Prompts{
interaction_result := "expect \"" + cmd.Interaction.Prompts[p_index] + "\"\nsend \""
temp_add := ""
for r_index := range cmd.Interaction.Replies[p_index].Litteral{
interaction_result += cmd.Interaction.Replies[p_index].Litteral[r_index]
temp_add = retrieve_mapped_value(cmd.Interaction.Replies[p_index].Variable[r_index], var_swap)
if temp_add != ""{ //this looks wasteful but it is needed for reasons I don't understand
temp_add += "\n" //the basic explaination is it seems `retrieve_mapped_value` appends a carriage return & this
temp_add = temp_add[:len(temp_add) - 2] //messes up the script, I don't know why it appends it, but removes it
}
interaction_result += temp_add
}
interaction_result += "\\r\"\nsleep " + sleep_duration + "\n"
result += interaction_result
}
}
if cmd.Expects != "" {
result += "expect \"" + cmd.Expects + "\"\n"
}
return result
}
关于arrays - 为什么连接这两个字符串会在 Go 中移动组合字符串的前 3 个字符的末尾 3 个字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55912728/