This commit is contained in:
Yun
2023-12-27 18:27:04 +08:00
commit 9d00782c7b
4 changed files with 70 additions and 0 deletions
+7
View File
@@ -0,0 +1,7 @@
module code.yun.ink/pkg/sshx
go 1.19
require golang.org/x/crypto v0.15.0
require golang.org/x/sys v0.14.0 // indirect
+5
View File
@@ -0,0 +1,5 @@
golang.org/x/crypto v0.15.0 h1:frVn1TEaCEaZcn3Tmd7Y2b5KKPaZ+I32Q2OA3kYp5TA=
golang.org/x/crypto v0.15.0/go.mod h1:4ChreQoLWfG3xLDer1WdlH5NdlQ3+mwnQq1YTKY+72g=
golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q=
golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.14.0 h1:LGK9IlZ8T9jvdy6cTdfKUCltatMFOehAQo9SRC46UQ8=
+56
View File
@@ -0,0 +1,56 @@
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
}
+2
View File
@@ -0,0 +1,2 @@
package sshx