Guest User

Untitled

a guest
Jul 23rd, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.16 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "fmt"
  5. "io"
  6. "io/ioutil"
  7. "net"
  8. "os"
  9. "strings"
  10.  
  11. "golang.org/x/crypto/ssh"
  12. "golang.org/x/crypto/ssh/agent"
  13. )
  14.  
  15. type SSHCommand struct {
  16. Path string
  17. Env []string
  18. Stdin io.Reader
  19. Stdout io.Writer
  20. Stderr io.Writer
  21. }
  22.  
  23. type SSHClient struct {
  24. Config *ssh.ClientConfig
  25. Host string
  26. Port int
  27. }
  28.  
  29. func (client *SSHClient) RunCommand(cmd *SSHCommand) error {
  30. var (
  31. session *ssh.Session
  32. err error
  33. )
  34.  
  35. if session, err = client.newSession(); err != nil {
  36. return err
  37. }
  38. defer session.Close()
  39.  
  40. if err = client.prepareCommand(session, cmd); err != nil {
  41. return err
  42. }
  43.  
  44. err = session.Run(cmd.Path)
  45. return err
  46. }
  47.  
  48. func (client *SSHClient) prepareCommand(session *ssh.Session, cmd *SSHCommand) error {
  49. for _, env := range cmd.Env {
  50. variable := strings.Split(env, "=")
  51. if len(variable) != 2 {
  52. continue
  53. }
  54.  
  55. if err := session.Setenv(variable[0], variable[1]); err != nil {
  56. return err
  57. }
  58. }
  59.  
  60. if cmd.Stdin != nil {
  61. stdin, err := session.StdinPipe()
  62. if err != nil {
  63. return fmt.Errorf("Unable to setup stdin for session: %v", err)
  64. }
  65. go io.Copy(stdin, cmd.Stdin)
  66. }
  67.  
  68. if cmd.Stdout != nil {
  69. stdout, err := session.StdoutPipe()
  70. if err != nil {
  71. return fmt.Errorf("Unable to setup stdout for session: %v", err)
  72. }
  73. go io.Copy(cmd.Stdout, stdout)
  74. }
  75.  
  76. if cmd.Stderr != nil {
  77. stderr, err := session.StderrPipe()
  78. if err != nil {
  79. return fmt.Errorf("Unable to setup stderr for session: %v", err)
  80. }
  81. go io.Copy(cmd.Stderr, stderr)
  82. }
  83.  
  84. return nil
  85. }
  86.  
  87. func (client *SSHClient) newSession() (*ssh.Session, error) {
  88. connection, err := ssh.Dial("tcp", fmt.Sprintf("%s:%d", client.Host, client.Port), client.Config)
  89. if err != nil {
  90. return nil, fmt.Errorf("Failed to dial: %s", err)
  91. }
  92.  
  93. session, err := connection.NewSession()
  94. if err != nil {
  95. return nil, fmt.Errorf("Failed to create session: %s", err)
  96. }
  97.  
  98. modes := ssh.TerminalModes{
  99. // ssh.ECHO: 0, // disable echoing
  100. ssh.TTY_OP_ISPEED: 14400, // input speed = 14.4kbaud
  101. ssh.TTY_OP_OSPEED: 14400, // output speed = 14.4kbaud
  102. }
  103.  
  104. if err := session.RequestPty("xterm", 80, 40, modes); err != nil {
  105. session.Close()
  106. return nil, fmt.Errorf("request for pseudo terminal failed: %s", err)
  107. }
  108.  
  109. return session, nil
  110. }
  111.  
  112. func PublicKeyFile(file string) ssh.AuthMethod {
  113. buffer, err := ioutil.ReadFile(file)
  114. if err != nil {
  115. return nil
  116. }
  117.  
  118. key, err := ssh.ParsePrivateKey(buffer)
  119. if err != nil {
  120. return nil
  121. }
  122. return ssh.PublicKeys(key)
  123. }
  124.  
  125. func SSHAgent() ssh.AuthMethod {
  126. if sshAgent, err := net.Dial("unix", os.Getenv("SSH_AUTH_SOCK")); err == nil {
  127. return ssh.PublicKeysCallback(agent.NewClient(sshAgent).Signers)
  128. }
  129. return nil
  130. }
  131.  
  132. func main() {
  133. // ssh.Password("your_password")
  134. sshConfig := &ssh.ClientConfig{
  135. User: "jsmith",
  136. Auth: []ssh.AuthMethod{
  137. SSHAgent(),
  138. },
  139. }
  140.  
  141. client := &SSHClient{
  142. Config: sshConfig,
  143. Host: "example.com",
  144. Port: 22,
  145. }
  146.  
  147. cmd := &SSHCommand{
  148. Path: "ls -l $LC_DIR",
  149. Env: []string{"LC_DIR=/"},
  150. Stdin: os.Stdin,
  151. Stdout: os.Stdout,
  152. Stderr: os.Stderr,
  153. }
  154.  
  155. fmt.Printf("Running command: %s\n", cmd.Path)
  156. if err := client.RunCommand(cmd); err != nil {
  157. fmt.Fprintf(os.Stderr, "command run error: %s\n", err)
  158. os.Exit(1)
  159. }
  160. }
Add Comment
Please, Sign In to add comment