duanliusong6395 2019-04-30 01:19
浏览 50
已采纳

为什么在Go中合并这两个字符串会使结尾的3个字符移动到组合字符串的前3个字符之上?

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.

  • 写回答

1条回答 默认 最新

  • donpvtzuux37724 2019-05-03 22:10
    关注

    I never found the solution to this but the temp_add section of the below where I arbitrarily slice off the end of the string works. There is some voodoo up in here.

    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{
                interaction_result := "expect \"" + cmd.Interaction.Prompts[p_index] + "\"
    send \""
                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 += "
    "    //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 += "\\"
    sleep " + sleep_duration + "
    "
                result += interaction_result
            }
        }
        if cmd.Expects != "" {
            result += "expect \"" + cmd.Expects + "\"
    "
        }
        return result
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办