commit 9d00782c7b50073fbdc8b073362aee2b756c5fc2 Author: Yun Date: Wed Dec 27 18:27:04 2023 +0800 提交 diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..43f6a67 --- /dev/null +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..df85b7b --- /dev/null +++ b/go.sum @@ -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= diff --git a/sshx.go b/sshx.go new file mode 100644 index 0000000..0bf9ace --- /dev/null +++ b/sshx.go @@ -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 +} diff --git a/sshx_test.go b/sshx_test.go new file mode 100644 index 0000000..d7738d8 --- /dev/null +++ b/sshx_test.go @@ -0,0 +1,2 @@ +package sshx +