github.com/ocurr/cryorio@v0.0.0-20220116160810-2fb94073801b/roborio/ssh_conn.go (about)

     1  package roborio
     2  
     3  import (
     4  	"golang.org/x/crypto/ssh"
     5  )
     6  
     7  func DialSsh(user, pass, addr string) (Conn, error) {
     8  	config := &ssh.ClientConfig{
     9  		User: user,
    10  		Auth: []ssh.AuthMethod{
    11  			ssh.Password(pass),
    12  		},
    13  		HostKeyCallback: ssh.InsecureIgnoreHostKey(),
    14  	}
    15  	client, err := ssh.Dial("tcp", addr, config)
    16  	if err != nil {
    17  		return nil, err
    18  	}
    19  	return &SshConn{client}, nil
    20  }
    21  
    22  type SshConn struct {
    23  	client *ssh.Client
    24  }
    25  
    26  func (s *SshConn) Close() error {
    27  	return s.client.Close()
    28  }
    29  
    30  func (s *SshConn) Exec(command string) ([]byte, error) {
    31  	session, err := s.client.NewSession()
    32  	if err != nil {
    33  		return []byte{}, err
    34  	}
    35  	out, err := session.CombinedOutput(command)
    36  	if err != nil {
    37  		return []byte{}, err
    38  	}
    39  	return out, nil
    40  }