前端之家收集整理的这篇文章主要介绍了
GO语言使用开源SSH模拟终端,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
<pre name="code" class="plain">package main
import (
"go-ssh/ssh"
"log"
"os"
)
func main() {
check := func(err error,msg string) {
if err != nil {
log.Fatalf("%s error: %v",msg,err)
}
}
client,err := ssh.Dial("tcp","192.168.10.248",&ssh.ClientConfig{
User: "root",Auth: []ssh.AuthMethod{ssh.Password("123456")},})
check(err,"dial")
session,err := client.NewSession()
check(err,"new session")
defer session.Close()
session.Stdout = os.Stdout
session.Stderr = os.Stderr
session.Stdin = os.Stdin
modes := ssh.TerminalModes{
ssh.ECHO: 0,ssh.TTY_OP_ISPEED: 14400,ssh.TTY_OP_OSPEED: 14400,}
err = session.RequestPty("xterm",25,100,modes)
check(err,"request pty")
err = session.Shell()
check(err,"start shell")
err = session.Wait()
check(err,"return")
}
原文链接:https://www.f2er.com/go/190329.html