Files
2023-12-27 18:27:04 +08:00

57 lines
1.1 KiB
Go

package sshx
import (
"go_mysqldump/global"
"log"
"golang.org/x/crypto/ssh"
)
type SshxClient struct {
*ssh.Client
}
type SshxSession struct {
*ssh.Session
}
// 用完需要Close
type Config struct {
Host string `json:"host" yaml:"host"`
User string `json:"user" yaml:"user"`
PassWord string `json:"password" yaml:"password"`
Port int `json:"port" yaml:"port"`
}
func NewClient(conf Config) (*SshxClient, error) {
config := &ssh.ClientConfig{
User: conf.User,
Auth: []ssh.AuthMethod{
ssh.Password(conf.Password),
},
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
}
client, err := ssh.Dial("tcp", conf.Host+":"+conf.Port, config)
if err != nil {
log.Fatal("ssh Failed to dail:", err)
}
// defer client.Close()
s := SshxClient{client}
return &s, err
}
// 获取Session
func (cc *SshxClient) NewSession() (*SshxSession, error) {
session, err := cc.NewSession()
if err != nil {
log.Fatal("Fail to dial:", err)
}
// defer session.Close()
s := SshxSession{session}
return &s, err
}