go - 如何在 go cli 函数期间接受一串文本?
问题描述
我正在尝试通过将 exec.Command 添加到 cli 插件来开发 PoC,但是我正在努力如何让它接受用户输入的命令。我承认我对 Go 很陌生,来自 PHP 和 Bash 脚本,所以......我确信我在这里遗漏了一些非常基本的东西,这有点尴尬。
我尝试设置变量,将变量添加到输出,添加 bufio,并尝试提示但没有运气。
out, err := exec.Command("mtr", "-c", "10", "-r", destIP).Output()
是最近的失败...
type Mtr struct{}
func (m *Mtr) Run(command []string, context plugin.PluginContext, ui terminal.UI) {
// var destIP string - commented out simply so I could rebuild the file and get the plugin working again.
ui.Say("")
ui.Say(terminal.AdvisoryColor("Wait while we run a traceroute..."))
ui.Say("")
out, err := exec.Command("mtr", "-c", "10", "-r").Output()
if err != nil {
ui.Say(fmt.Sprintf("%s", err))
}
output := string(out[:])
table := ui.Table([]string{"", ""})
table.Add("", output)
table.Print()
我真的只是希望它能够接受这样的事情
mainprogram plugin mtr 目前我将其设置为仅强制 Google,但这并不是我真正想要的...
out, err := exec.Command("mtr", "-c", "10", "-r", "8.8.8.8").Output()
尝试失败的当前输出产生 -
Shawns-MBP-2:directoryhere $ mainprog plugin mtr 4.4.4.4
Wait while we run a traceroute...
Shawns-MBP-2:directoryhere$
With the forced Google DNS, it obviously shows -
Shawns-MBP-2:directoryhere $ mainprog plugin mtr
Wait while we run a traceroute...
Start: 2019-05-03T15:41:18-0500
HOST: stuff Loss% Snt Last Avg Best Wrst StDev
1.|-- stuff 0.0% 10 1.6 1.7 1.2 3.1 0.5
2.|-- stuff 0.0% 10 2.6 6.5 2.0 12.4 4.5
3.|-- stuff 0.0% 10 3.3 3.3 2.7 4.1 0.4
4.|-- stuff 0.0% 10 13.6 12.2 9.1 15.6 2.5
5.|-- stuff 0.0% 10 9.2 12.9 9.2 17.7 2.5
6.|-- stuff 0.0% 10 8.7 9.1 8.0 10.5 0.6
7.|-- stuff 0.0% 10 9.1 9.2 8.2 10.1 0.5
8.|-- stuff 0.0% 10 10.3 10.1 9.7 10.4 0.3
9.|-- stuff 0.0% 10 8.8 9.2 8.5 10.7 0.6
10.|-- google-public-dns-a.googl 0.0% 10 8.7 8.9 8.2 10.0 0.4
解决方案
在被指出了正确的方向之后,终于想通了——
dest := os.Args[2]
out, err := exec.Command("mtr", "-c", "10", "-r", dest).Output()
这使它能够接受输入并验证它接受 IP 和域。这开辟了一个全新的可能性世界:D
推荐阅读
- node.js - 构建好,但部署不起作用/ Heroku & gitlab
- javascript - 如何在Javascript中合并2个具有不同键名的数组对象?
- javascript - 如何在 Ant.Design 中设置没有表单对象的 Form.Item 的值?
- gstreamer - 带有 T 形棒的 Gstreamer RTSP 服务器管道
- angular - svg 上的 Cypress 角测试样式内联
- ruby - 如何将字符串转换为嵌套哈希
- spring-boot - 无法为我的 Spring Boot 应用程序获取 Shibboleth 断言属性
- c# - foreach 头部中的 LINQ
- docker - 中介代理 - Hyperledger.Indy.IOException:发生 IO 错误
- java - java webapp 模块支持