Skip to content

Instantly share code, notes, and snippets.

@metarsit
Created July 10, 2022 05:07
Show Gist options
  • Select an option

  • Save metarsit/dfa6633bc9e80c67ac98a9f411e0c0aa to your computer and use it in GitHub Desktop.

Select an option

Save metarsit/dfa6633bc9e80c67ac98a9f411e0c0aa to your computer and use it in GitHub Desktop.
package medium
import (
"net"
"golang.org/x/crypto/ssh"
)
func RunCmdOverSSH(addr, username, password, cmd string) ([]byte, error) {
cfg := &ssh.ClientConfig{
User: username,
Auth: []ssh.AuthMethod{
ssh.Password(password),
},
HostKeyCallback: ssh.HostKeyCallback(
func(hostname string, remote net.Addr, key ssh.PublicKey) error {
return nil
},
),
}
conn, err := ssh.Dial("tcp", addr, cfg)
if err != nil {
return nil, err
}
session, err := conn.NewSession()
if err != nil {
return nil, err
}
defer session.Close()
return session.CombinedOutput(cmd)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment