golang调用模组程序实现交互输入自动化,获取imei及iccid
应用场景:在openwrt下调用移远的测试程序,并实现输入自动话,获取imei
root@OpenWrt:~# ql-api-test
Test groups:
0: ql_dsi
1: ql_nw
2: ql_sim
3: ql_dev
4: ql_voice
5: ql_sms
6: ql_adc
7: ql_i2c
8: ql_enit
9: ql_fota
10: ql_gpio
11: ql_spi
12: ql_uart
13: ql_locator
14: ql_lpm
15: ql_audio
16: ql_wifi
17: ql_atc
please input command index(-1 exit):
3
Group Name:ql_dev, Supported test cases:
0: ql_dev_init
1: ql_dev_release
2: ql_dev_get_imei
3: ql_dev_get_firmware_version
4: ql_dev_get_model
5: ql_dev_get_sn
6: ql_dev_set_modem_fun
7: ql_dev_get_modem_fun
8: ql_dev_forbidind
please input cmd index(-1 exit):
0
ql_dev_init ret = 0
please input cmd index(-1 exit):
2
ql_dev_get_imei ret = 0, imei:860503070780822;
please input cmd index(-1 exit):
-1
Test groups:
0: ql_dsi
1: ql_nw
2: ql_sim
3: ql_dev
4: ql_voice
5: ql_sms
6: ql_adc
7: ql_i2c
8: ql_enit
9: ql_fota
10: ql_gpio
11: ql_spi
12: ql_uart
13: ql_locator
14: ql_lpm
15: ql_audio
16: ql_wifi
17: ql_atc
please input command index(-1 exit):
-1
自动化指令:
方式1
echo -e "3\n0\n2\n-1\n-1" | ql-api-test
方式2
ql-api-test <<EOF
3
0
2
-1
-1
EOF
golang代码实现
process.go
package util import ( "bytes" "os/exec" "regexp" "runtime" "strings" "tboxconfig/util/common" ) // 检查进程是否在运行(适用于类Unix系统和Windows) func GetIMEI() string { imei := "" if runtime.GOOS == "windows" { return "不支持windows获取MEI" } else { echoCmd := exec.Command("echo", "-e", "3\n0\n2\n-1\n-1") // 创建 ql-api-test 命令 qlApiTestCmd := exec.Command("ql-api-test") // 将 echo 命令的输出作为 ql-api-test 命令的输入 echoOut, _ := echoCmd.StdoutPipe() qlApiTestCmd.Stdin = echoOut // 捕获 ql-api-test 命令的标准输出 var stdout bytes.Buffer qlApiTestCmd.Stdout = &stdout // 启动 echo 命令 if err := echoCmd.Start(); err != nil { common.LogError2("启动 echo 命令失败:", err) return "" } // 启动 ql-api-test 命令 if err := qlApiTestCmd.Start(); err != nil { common.LogError2("启动 ql-api-test 命令失败:", err) return "" } // 等待 echo 命令完成 if err := echoCmd.Wait(); err != nil { common.LogError2("echo 命令执行失败:", err) return "" } // 等待 ql-api-test 命令完成 if err := qlApiTestCmd.Wait(); err != nil { common.LogError2("ql-api-test 命令执行失败:", err) } // 获取标准输出的内容 output := stdout.String() common.LogInfo("imei输出:" + output) // 使用正则表达式提取 imei re := regexp.MustCompile(`imei:([0-9]+);`) matches := re.FindStringSubmatch(output) if len(matches) > 1 { imei = matches[1] } } return imei } func GetICCID() string { iccid := "" if runtime.GOOS == "windows" { return "不支持windows获取ICCID" } else { cmd := exec.Command("serial_atcmd", "AT+QCCID") // 捕获命令的标准输出和标准错误输出 var stdout bytes.Buffer var stderr bytes.Buffer cmd.Stdout = &stdout cmd.Stderr = &stderr // 启动命令 if err := cmd.Run(); err != nil { common.LogError2("命令执行失败:", err) return "" } output := stdout.String() common.LogInfo("iccid输出:" + output) // 提取 ICCID iccid = extractICCID(output) } return iccid } // 从输出中提取 ICCID func extractICCID(output string) string { // 假设输出格式为 "+QCCID: 89860012345678901234" 或类似格式 lines := strings.Split(output, "\n") for _, line := range lines { if strings.HasPrefix(line, "+QCCID:") { parts := strings.Split(line, ":") if len(parts) > 1 { return strings.TrimSpace(parts[1]) } } } return "" }
qq:505645074