Created
June 23, 2022 10:10
-
-
Save fengyfei/4b552e50264c2c124cbdd0af689db2b4 to your computer and use it in GitHub Desktop.
[Go] SSH Password Mode
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 main | |
import ( | |
"bytes" | |
"fmt" | |
"log" | |
"golang.org/x/crypto/ssh" | |
) | |
func main() { | |
config := &ssh.ClientConfig{ | |
User: "User", | |
Auth: []ssh.AuthMethod{ | |
ssh.Password("Password"), | |
}, | |
HostKeyCallback: ssh.InsecureIgnoreHostKey(), | |
} | |
client, err := ssh.Dial("tcp", "192.168.0.251:22", config) | |
if err != nil { | |
log.Fatal("Failed to dial: ", err) | |
} | |
defer client.Close() | |
session, err := client.NewSession() | |
if err != nil { | |
log.Fatal("Failed to create session: ", err) | |
} | |
defer session.Close() | |
var b bytes.Buffer | |
session.Stdout = &b | |
if err := session.Run("whoami"); err != nil { | |
log.Fatal("Failed to run: " + err.Error()) | |
} | |
fmt.Println(b.String()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment