example_test.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. // Copyright 2011 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package ssh_test
  5. import (
  6. "bytes"
  7. "fmt"
  8. "io/ioutil"
  9. "log"
  10. "net"
  11. "net/http"
  12. "golang.org/x/crypto/ssh"
  13. "golang.org/x/crypto/ssh/terminal"
  14. )
  15. func ExampleNewServerConn() {
  16. // An SSH server is represented by a ServerConfig, which holds
  17. // certificate details and handles authentication of ServerConns.
  18. config := &ssh.ServerConfig{
  19. PasswordCallback: func(c ssh.ConnMetadata, pass []byte) (*ssh.Permissions, error) {
  20. // Should use constant-time compare (or better, salt+hash) in
  21. // a production setting.
  22. if c.User() == "testuser" && string(pass) == "tiger" {
  23. return nil, nil
  24. }
  25. return nil, fmt.Errorf("password rejected for %q", c.User())
  26. },
  27. }
  28. privateBytes, err := ioutil.ReadFile("id_rsa")
  29. if err != nil {
  30. log.Fatal("Failed to load private key: ", err)
  31. }
  32. private, err := ssh.ParsePrivateKey(privateBytes)
  33. if err != nil {
  34. log.Fatal("Failed to parse private key: ", err)
  35. }
  36. config.AddHostKey(private)
  37. // Once a ServerConfig has been configured, connections can be
  38. // accepted.
  39. listener, err := net.Listen("tcp", "0.0.0.0:2022")
  40. if err != nil {
  41. log.Fatal("failed to listen for connection: ", err)
  42. }
  43. nConn, err := listener.Accept()
  44. if err != nil {
  45. log.Fatal("failed to accept incoming connection: ", err)
  46. }
  47. // Before use, a handshake must be performed on the incoming
  48. // net.Conn.
  49. _, chans, reqs, err := ssh.NewServerConn(nConn, config)
  50. if err != nil {
  51. log.Fatal("failed to handshake: ", err)
  52. }
  53. // The incoming Request channel must be serviced.
  54. go ssh.DiscardRequests(reqs)
  55. // Service the incoming Channel channel.
  56. for newChannel := range chans {
  57. // Channels have a type, depending on the application level
  58. // protocol intended. In the case of a shell, the type is
  59. // "session" and ServerShell may be used to present a simple
  60. // terminal interface.
  61. if newChannel.ChannelType() != "session" {
  62. newChannel.Reject(ssh.UnknownChannelType, "unknown channel type")
  63. continue
  64. }
  65. channel, requests, err := newChannel.Accept()
  66. if err != nil {
  67. log.Fatal("could not accept channel: ", err)
  68. }
  69. // Sessions have out-of-band requests such as "shell",
  70. // "pty-req" and "env". Here we handle only the
  71. // "shell" request.
  72. go func(in <-chan *ssh.Request) {
  73. for req := range in {
  74. ok := false
  75. switch req.Type {
  76. case "shell":
  77. ok = true
  78. if len(req.Payload) > 0 {
  79. // We don't accept any
  80. // commands, only the
  81. // default shell.
  82. ok = false
  83. }
  84. }
  85. req.Reply(ok, nil)
  86. }
  87. }(requests)
  88. term := terminal.NewTerminal(channel, "> ")
  89. go func() {
  90. defer channel.Close()
  91. for {
  92. line, err := term.ReadLine()
  93. if err != nil {
  94. break
  95. }
  96. fmt.Println(line)
  97. }
  98. }()
  99. }
  100. }
  101. func ExampleDial() {
  102. // An SSH client is represented with a ClientConn.
  103. //
  104. // To authenticate with the remote server you must pass at least one
  105. // implementation of AuthMethod via the Auth field in ClientConfig.
  106. config := &ssh.ClientConfig{
  107. User: "username",
  108. Auth: []ssh.AuthMethod{
  109. ssh.Password("yourpassword"),
  110. },
  111. }
  112. client, err := ssh.Dial("tcp", "yourserver.com:22", config)
  113. if err != nil {
  114. log.Fatal("Failed to dial: ", err)
  115. }
  116. // Each ClientConn can support multiple interactive sessions,
  117. // represented by a Session.
  118. session, err := client.NewSession()
  119. if err != nil {
  120. log.Fatal("Failed to create session: ", err)
  121. }
  122. defer session.Close()
  123. // Once a Session is created, you can execute a single command on
  124. // the remote side using the Run method.
  125. var b bytes.Buffer
  126. session.Stdout = &b
  127. if err := session.Run("/usr/bin/whoami"); err != nil {
  128. log.Fatal("Failed to run: " + err.Error())
  129. }
  130. fmt.Println(b.String())
  131. }
  132. func ExamplePublicKeys() {
  133. // A public key may be used to authenticate against the remote
  134. // server by using an unencrypted PEM-encoded private key file.
  135. //
  136. // If you have an encrypted private key, the crypto/x509 package
  137. // can be used to decrypt it.
  138. key, err := ioutil.ReadFile("/home/user/.ssh/id_rsa")
  139. if err != nil {
  140. log.Fatalf("unable to read private key: %v", err)
  141. }
  142. // Create the Signer for this private key.
  143. signer, err := ssh.ParsePrivateKey(key)
  144. if err != nil {
  145. log.Fatalf("unable to parse private key: %v", err)
  146. }
  147. config := &ssh.ClientConfig{
  148. User: "user",
  149. Auth: []ssh.AuthMethod{
  150. // Use the PublicKeys method for remote authentication.
  151. ssh.PublicKeys(signer),
  152. },
  153. }
  154. // Connect to the remote server and perform the SSH handshake.
  155. client, err := ssh.Dial("tcp", "host.com:22", config)
  156. if err != nil {
  157. log.Fatalf("unable to connect: %v", err)
  158. }
  159. defer client.Close()
  160. }
  161. func ExampleClient_Listen() {
  162. config := &ssh.ClientConfig{
  163. User: "username",
  164. Auth: []ssh.AuthMethod{
  165. ssh.Password("password"),
  166. },
  167. }
  168. // Dial your ssh server.
  169. conn, err := ssh.Dial("tcp", "localhost:22", config)
  170. if err != nil {
  171. log.Fatal("unable to connect: ", err)
  172. }
  173. defer conn.Close()
  174. // Request the remote side to open port 8080 on all interfaces.
  175. l, err := conn.Listen("tcp", "0.0.0.0:8080")
  176. if err != nil {
  177. log.Fatal("unable to register tcp forward: ", err)
  178. }
  179. defer l.Close()
  180. // Serve HTTP with your SSH server acting as a reverse proxy.
  181. http.Serve(l, http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
  182. fmt.Fprintf(resp, "Hello world!\n")
  183. }))
  184. }
  185. func ExampleSession_RequestPty() {
  186. // Create client config
  187. config := &ssh.ClientConfig{
  188. User: "username",
  189. Auth: []ssh.AuthMethod{
  190. ssh.Password("password"),
  191. },
  192. }
  193. // Connect to ssh server
  194. conn, err := ssh.Dial("tcp", "localhost:22", config)
  195. if err != nil {
  196. log.Fatal("unable to connect: ", err)
  197. }
  198. defer conn.Close()
  199. // Create a session
  200. session, err := conn.NewSession()
  201. if err != nil {
  202. log.Fatal("unable to create session: ", err)
  203. }
  204. defer session.Close()
  205. // Set up terminal modes
  206. modes := ssh.TerminalModes{
  207. ssh.ECHO: 0, // disable echoing
  208. ssh.TTY_OP_ISPEED: 14400, // input speed = 14.4kbaud
  209. ssh.TTY_OP_OSPEED: 14400, // output speed = 14.4kbaud
  210. }
  211. // Request pseudo terminal
  212. if err := session.RequestPty("xterm", 80, 40, modes); err != nil {
  213. log.Fatal("request for pseudo terminal failed: ", err)
  214. }
  215. // Start remote shell
  216. if err := session.Shell(); err != nil {
  217. log.Fatal("failed to start shell: ", err)
  218. }
  219. }