Created
July 10, 2022 05:07
-
-
Save metarsit/dfa6633bc9e80c67ac98a9f411e0c0aa to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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