Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "fmt"
- "log"
- "os"
- "io/ioutil"
- "golang.org/x/crypto/ssh"
- )
- func main() {
- if len(os.Args) != 4 {
- log.Fatalf("Usage: %s <user> <host:port> <command>", os.Args[0])
- }
- client, session, err := connectToHost(os.Args[1], os.Args[2])
- if err != nil {
- panic(err)
- }
- out, err := session.CombinedOutput("ls")
- if err != nil {
- panic(err)
- }
- fmt.Println(string(out))
- client.Close()
- }
- func PublicKeyFile(file string) (ssh.AuthMethod, error) {
- buffer, err := ioutil.ReadFile(file)
- if err != nil {
- return nil, err
- }
- key, err := ssh.ParsePrivateKey(buffer)
- if err != nil {
- return nil, err
- }
- return ssh.PublicKeys(key), nil
- }
- func connectToHost(user, host string) (*ssh.Client, *ssh.Session, error) {
- var pass string
- fmt.Print("Password: ")
- fmt.Scanf("%sn", &pass)
- publicKey, err := PublicKeyFile(`./key.pem`)
- if err != nil {
- log.Println(err)
- }
- sshConfig := &ssh.ClientConfig{
- User: user,
- Auth: []ssh.AuthMethod{publicKey},
- }
- sshConfig.HostKeyCallback = ssh.InsecureIgnoreHostKey()
- client, err := ssh.Dial("tcp", host, sshConfig)
- if err != nil {
- return nil, nil, err
- }
- session, err := client.NewSession()
- if err != nil {
- client.Close()
- return nil, nil, err
- }
- return client, session, nil
- }
Add Comment
Please, Sign In to add comment