提交
This commit is contained in:
@@ -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
|
||||
@@ -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=
|
||||
@@ -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
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
package sshx
|
||||
|
||||
Reference in New Issue
Block a user