Guest User

Untitled

a guest
Dec 3rd, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.90 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "fmt"
  5. "log"
  6. "os"
  7. "golang.org/x/crypto/ssh"
  8. )
  9.  
  10. func main() {
  11. if len(os.Args) != 4 {
  12. log.Fatalf("Usage: %s <user> <host:port> <command>", os.Args[0])
  13. }
  14.  
  15. client, session, err := connectToHost(os.Args[1], os.Args[2])
  16. if err != nil {
  17. panic(err)
  18. }
  19. out, err := session.CombinedOutput(os.Args[3])
  20. if err != nil {
  21. panic(err)
  22. }
  23. fmt.Println(string(out))
  24. client.Close()
  25. }
  26.  
  27. func connectToHost(user, host string) (*ssh.Client, *ssh.Session, error) {
  28. var pass string
  29. fmt.Print("Password: ")
  30. fmt.Scanf("%s\n", &pass)
  31.  
  32. sshConfig := &ssh.ClientConfig{
  33. User: user,
  34. Auth: []ssh.AuthMethod{ssh.Password(pass)},
  35. }
  36. sshConfig.HostKeyCallback = ssh.InsecureIgnoreHostKey()
  37.  
  38. client, err := ssh.Dial("tcp", host, sshConfig)
  39. if err != nil {
  40. return nil, nil, err
  41. }
  42.  
  43. session, err := client.NewSession()
  44. if err != nil {
  45. client.Close()
  46. return nil, nil, err
  47. }
  48.  
  49. return client, session, nil
  50. }
Add Comment
Please, Sign In to add comment