第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

Golang Exec 命令長(zhǎng)輸出

Golang Exec 命令長(zhǎng)輸出

Go
斯蒂芬大帝 2022-04-20 21:06:50
如何從很長(zhǎng)的 ping 命令輸出中獲取 ping 統(tǒng)計(jì)信息:func main() {    cmdout, _ := exec.Command("ping", "127.0.0.1", "-n", "30000").Output()    fmt.Printf("%s\n", cmdout)}我只需要這個(gè)輸出:Ping statistics for 127.0.0.1:    Packets: Sent = 30000, Received = 30000, Lost = 0 (0% loss),Approximate round trip times in milli-seconds:    Minimum = 0ms, Maximum = 0ms, Average = 0ms輸出如下:Reply from 127.0.0.1: bytes=32 time<1ms TTL=128Reply from 127.0.0.1: bytes=32 time<1ms TTL=128Reply from 127.0.0.1: bytes=32 time<1ms TTL=128...............................................我只想丟棄。我正在考慮將所有這些輸出放入一個(gè)變量中,然后對(duì)其進(jìn)行解析,直到我得到所需的結(jié)果:output := string(out)    scanner := bufio.NewScanner(strings.NewReader(output))    for scanner.Scan() {        fmt.Println("Line: ", scanner.Text())        regex compile etc...    }但是,我不確定這是實(shí)現(xiàn)這一目標(biāo)的有效模式,通過(guò)選擇這種方式意味著用大量未使用的數(shù)據(jù)填充 RAM,這不是我在看的。我說(shuō)的對(duì)嗎?
查看完整描述

1 回答

?
皈依舞

TA貢獻(xiàn)1851條經(jīng)驗(yàn) 獲得超3個(gè)贊

我認(rèn)為以下代碼是我需要的:


func main() {

    args := "-n 30000 127.0.0.1"

    cmd := exec.Command("ping", strings.Split(args, " ")...)


    output, _ := cmd.StdoutPipe()

    cmd.Start()


    scanner := bufio.NewScanner(output)

    for scanner.Scan() {

        m := scanner.Text()


        matchPackets, _ := regexp.MatchString("Packets", m)

        matchMinimum, _ := regexp.MatchString("Minimum", m)


        if matchPackets {

            fmt.Println("Ping statistics for 127.0.0.1")

            fmt.Println(m)

        }


        if matchMinimum {

            fmt.Println("Approximate round trip times in milli-seconds:")

            fmt.Println(m)

        }

    }

    cmd.Wait()

}


查看完整回答
反對(duì) 回復(fù) 2022-04-20
  • 1 回答
  • 0 關(guān)注
  • 165 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)