package main import ( "github.com/dynport/gossh" "log" ) func MakeLogger(prefix string) gossh.Writer { return func(args ...interface{}) { log.Println((append([]interface{}{prefix}, args...))...) } } func main() { client := gossh.New("192.168.1.106", "mmc") client.SetPassword("123456") client.DebugWriter = MakeLogger("DEBUG") client.InfoWriter = MakeLogger("INFO ") client.ErrorWriter = MakeLogger("ERROR") defer client.Close() rsp, e := client.Execute("ls") if e != nil { client.ErrorWriter(e.Error()) }else { client.InfoWriter(rsp.String()) } }
有疑问加站长微信联系(非本文作者)