go flag.Parse() expect

package main

import (
        "fmt"
        "flag"
        gexpect "github.com/ThomasRooney/gexpect"
)
func main() {
        cmdStr := flag.String("cmd", "", "cmds")
        hostStr := flag.String("host", "", "hoststr")
        passwd := flag.String("p", "", "passwd")
        flag.Parse()

        command := "ssh -o 'StrictHostKeyChecking no' -o ConnectTimeout=2 " + *hostStr + " -a " + *cmdStr
        child, err := gexpect.Spawn(command)
        if err != nil {
                panic(err)
        }
        fmt.Println("-----")
        child.Expect("password:")
        child.SendLine(*passwd)
        child.Interact()
        child.Close()
}

posted @ 2021-01-27 19:04  yangxl-dev  阅读(423)  评论(0)    收藏  举报