```go
package main
import (
"bytes"
"fmt"
"golang.org/x/crypto/ssh"
"os"
)
func main() {
var bt bytes.Buffer
config := &ssh.ClientConfig{
User: "test",
Auth: []ssh.AuthMethod{
ssh.Password("test"),
},
}
client, err := ssh.Dial("tcp", "10.0.192.123", config)
if err != nil {
os.Exit(1)
}
session, err := client.NewSession()
if err != nil {
os.Exit(1)
}
defer session.Close()
session.Stdout = &bt
if err := session.Run("/usr/bin/make"); err != nil {
os.Exit(1)
}
fmt.Println(bt.String()) //此处只能一次性读出结果,执行耗时长的命令时,半天无响应
}
```
```
session, err := conn.NewSession()
if err != nil {
log.Fatal("Create session fail.", err)
}
defer session.Close()
cmdReader, err := session.StdoutPipe()
if err != nil {
log.Fatal(err)
}
scanner := bufio.NewScanner(cmdReader)
go func() {
for scanner.Scan() {
fmt.Println(scanner.Text())
}
}()
session.Run(cmd)
```
#5
更多评论