栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Golang写入输入并从终端进程获取输出

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Golang写入输入并从终端进程获取输出

由于上面的评论,我可以使用密码使用ssh访问。我使用了golang的ssh api库。当我遵循以下示例时,这非常简单:

https://pre.google.com/p/go/source/browse/ssh/example_test.go?repo=crypto

特别:

func ExampleDial() {    // An SSH client is represented with a ClientConn. Currently only    // the "password" authentication method is supported.    //    // To authenticate with the remote server you must pass at least one    // implementation of AuthMethod via the Auth field in ClientConfig.    config := &ClientConfig{ User: "username", Auth: []AuthMethod{         Password("yourpassword"), },    }    client, err := Dial("tcp", "yourserver.com:22", config)    if err != nil { panic("Failed to dial: " + err.Error())    }    // Each ClientConn can support multiple interactive sessions,    // represented by a Session.    session, err := client.NewSession()    if err != nil { panic("Failed to create session: " + err.Error())    }    defer session.Close()    // once a Session is created, you can execute a single command on    // the remote side using the Run method.    var b bytes.Buffer    session.Stdout = &b    if err := session.Run("/usr/bin/whoami"); err != nil { panic("Failed to run: " + err.Error())    }    fmt.Println(b.String())}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/417741.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号