package model import ( "backend/common" "backend/util" "fmt" "golang.org/x/crypto/ssh" ) type SshClient struct { client *ssh.Client } func NewSshClient(config *common.SshConfig) (*SshClient, error) { SP, _ := util.Decrypt(config.Password) sshconfig := &ssh.ClientConfig{ User: config.Username, Auth: []ssh.AuthMethod{ ssh.Password(SP), }, HostKeyCallback: ssh.InsecureIgnoreHostKey(), } addr := fmt.Sprintf("%s:%d", config.Host, config.Port) client, err := ssh.Dial("tcp", addr, sshconfig) if err != nil { return nil, err } return &SshClient{client: client}, nil } func (s *SshClient) RunCommand(cmd string) (string, error) { session, err := s.client.NewSession() if err != nil { return "", err } defer session.Close() output, err := session.CombinedOutput(cmd) if err != nil { return "", err } return string(output), nil }