GTong 8 hónapja
commit
4a7963bb14
4 módosított fájl, 59 hozzáadás és 0 törlés
  1. 7 0
      go.mod
  2. 6 0
      go.sum
  3. 45 0
      internal/module/remote/remote.go
  4. 1 0
      main.go

+ 7 - 0
go.mod

@@ -0,0 +1,7 @@
+module xg_dba
+
+go 1.22.2
+
+require golang.org/x/crypto v0.27.0
+
+require golang.org/x/sys v0.25.0 // indirect

+ 6 - 0
go.sum

@@ -0,0 +1,6 @@
+golang.org/x/crypto v0.27.0 h1:GXm2NjJrPaiv/h1tb2UH8QfgC/hOf/+z0p6PT8o1w7A=
+golang.org/x/crypto v0.27.0/go.mod h1:1Xngt8kV6Dvbssa53Ziq6Eqn0HqbZi5Z6R0ZpwQzt70=
+golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34=
+golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
+golang.org/x/term v0.24.0 h1:Mh5cbb+Zk2hqqXNO7S1iTjEphVL+jb8ZWaqh/g+JWkM=
+golang.org/x/term v0.24.0/go.mod h1:lOBK/LVxemqiMij05LGJ0tzNr8xlmwBRJ81PX6wVLH8=

+ 45 - 0
internal/module/remote/remote.go

@@ -0,0 +1,45 @@
+package remote
+
+import "golang.org/x/crypto/ssh"
+
+// SSHClient 封装SSH连接配置
+type SSHClient struct {
+	Id       string
+	Username string
+	Password string
+	Host     string
+	Port     string
+	client   *ssh.Client
+}
+
+// NewSSHClient 创建SSH连接
+func NewSSHClient(Id, username, password, host, port string) (*SSHClient, error) {
+	config := &ssh.ClientConfig{
+		User: username,
+		Auth: []ssh.AuthMethod{
+			ssh.Password(password),
+		},
+		HostKeyCallback: ssh.InsecureIgnoreHostKey(),
+	}
+
+	client, err := ssh.Dial("tcp", host+":"+port, config)
+	if err != nil {
+		return nil, err
+	}
+
+	return &SSHClient{
+		Id:       Id,
+		Username: username,
+		Password: password,
+		Host:     host,
+		Port:     port,
+		client:   client,
+	}, nil
+}
+
+// Close 关闭SSH连接
+func (c *SSHClient) Close() {
+	if c.client != nil {
+		c.client.Close()
+	}
+}

+ 1 - 0
main.go

@@ -0,0 +1 @@
+package main