|
@@ -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()
|
|
|
+ }
|
|
|
+}
|