在go里面ssh连接有很多的库,但是实际我使用的体验感很差,最后搞定了,做了封装,舒适的使用就此开始
安装包
go install github.com/MrYZhou/outil
导入
import (
"fmt"
"os"
"strings"
"testing"
"time"
. "github.com/MrYZhou/outil/ssh"
)
使用密码连接
func TestConnect(t *testing.T) {
c, err := Server("192.168.0.68:22", "a06f6d1d64", "4b1d17d5bd")
if err != nil {
fmt.Println("连接失败")
} else {
fmt.Println(c)
}
}
使用密钥连接
func TestConnectWithKey(t *testing.T) {
// 读取私钥文件内容
contentBytes, _ := os.ReadFile("D:/root/68_rsa")
var cli Cli
cli.Host = "192.168.0.68:22"
cli.User = "root"
cli.PrivateKey = contentBytes
con, _ := ConnectServer(cli)
con.Run("pwd")
}
可以看到很丝滑的就完成了连接。操作方便