example_test.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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
  5. import (
  6. "bytes"
  7. "fmt"
  8. "io/ioutil"
  9. "log"
  10. "net/http"
  11. "code.google.com/p/go.crypto/ssh/terminal"
  12. )
  13. func ExampleListen() {
  14. // An SSH server is represented by a ServerConfig, which holds
  15. // certificate details and handles authentication of ServerConns.
  16. config := &ServerConfig{
  17. PasswordCallback: func(conn *ServerConn, user, pass string) bool {
  18. return user == "testuser" && pass == "tiger"
  19. },
  20. }
  21. privateBytes, err := ioutil.ReadFile("id_rsa")
  22. if err != nil {
  23. panic("Failed to load private key")
  24. }
  25. private, err := ParsePrivateKey(privateBytes)
  26. if err != nil {
  27. panic("Failed to parse private key")
  28. }
  29. config.AddHostKey(private)
  30. // Once a ServerConfig has been configured, connections can be
  31. // accepted.
  32. listener, err := Listen("tcp", "0.0.0.0:2022", config)
  33. if err != nil {
  34. panic("failed to listen for connection")
  35. }
  36. sConn, err := listener.Accept()
  37. if err != nil {
  38. panic("failed to accept incoming connection")
  39. }
  40. if err := sConn.Handshake(); err != nil {
  41. panic("failed to handshake")
  42. }
  43. // A ServerConn multiplexes several channels, which must
  44. // themselves be Accepted.
  45. for {
  46. // Accept reads from the connection, demultiplexes packets
  47. // to their corresponding channels and returns when a new
  48. // channel request is seen. Some goroutine must always be
  49. // calling Accept; otherwise no messages will be forwarded
  50. // to the channels.
  51. channel, err := sConn.Accept()
  52. if err != nil {
  53. panic("error from Accept")
  54. }
  55. // Channels have a type, depending on the application level
  56. // protocol intended. In the case of a shell, the type is
  57. // "session" and ServerShell may be used to present a simple
  58. // terminal interface.
  59. if channel.ChannelType() != "session" {
  60. channel.Reject(UnknownChannelType, "unknown channel type")
  61. continue
  62. }
  63. channel.Accept()
  64. term := terminal.NewTerminal(channel, "> ")
  65. serverTerm := &ServerTerminal{
  66. Term: term,
  67. Channel: channel,
  68. }
  69. go func() {
  70. defer channel.Close()
  71. for {
  72. line, err := serverTerm.ReadLine()
  73. if err != nil {
  74. break
  75. }
  76. fmt.Println(line)
  77. }
  78. }()
  79. }
  80. }
  81. func ExampleDial() {
  82. // An SSH client is represented with a ClientConn. Currently only
  83. // the "password" authentication method is supported.
  84. //
  85. // To authenticate with the remote server you must pass at least one
  86. // implementation of ClientAuth via the Auth field in ClientConfig.
  87. config := &ClientConfig{
  88. User: "username",
  89. Auth: []ClientAuth{
  90. // ClientAuthPassword wraps a ClientPassword implementation
  91. // in a type that implements ClientAuth.
  92. ClientAuthPassword(password("yourpassword")),
  93. },
  94. }
  95. client, err := Dial("tcp", "yourserver.com:22", config)
  96. if err != nil {
  97. panic("Failed to dial: " + err.Error())
  98. }
  99. // Each ClientConn can support multiple interactive sessions,
  100. // represented by a Session.
  101. session, err := client.NewSession()
  102. if err != nil {
  103. panic("Failed to create session: " + err.Error())
  104. }
  105. defer session.Close()
  106. // Once a Session is created, you can execute a single command on
  107. // the remote side using the Run method.
  108. var b bytes.Buffer
  109. session.Stdout = &b
  110. if err := session.Run("/usr/bin/whoami"); err != nil {
  111. panic("Failed to run: " + err.Error())
  112. }
  113. fmt.Println(b.String())
  114. }
  115. func ExampleClientConn_Listen() {
  116. config := &ClientConfig{
  117. User: "username",
  118. Auth: []ClientAuth{
  119. ClientAuthPassword(password("password")),
  120. },
  121. }
  122. // Dial your ssh server.
  123. conn, err := Dial("tcp", "localhost:22", config)
  124. if err != nil {
  125. log.Fatalf("unable to connect: %s", err)
  126. }
  127. defer conn.Close()
  128. // Request the remote side to open port 8080 on all interfaces.
  129. l, err := conn.Listen("tcp", "0.0.0.0:8080")
  130. if err != nil {
  131. log.Fatalf("unable to register tcp forward: %v", err)
  132. }
  133. defer l.Close()
  134. // Serve HTTP with your SSH server acting as a reverse proxy.
  135. http.Serve(l, http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
  136. fmt.Fprintf(resp, "Hello world!\n")
  137. }))
  138. }
  139. func ExampleSession_RequestPty() {
  140. // Create client config
  141. config := &ClientConfig{
  142. User: "username",
  143. Auth: []ClientAuth{
  144. ClientAuthPassword(password("password")),
  145. },
  146. }
  147. // Connect to ssh server
  148. conn, err := Dial("tcp", "localhost:22", config)
  149. if err != nil {
  150. log.Fatalf("unable to connect: %s", err)
  151. }
  152. defer conn.Close()
  153. // Create a session
  154. session, err := conn.NewSession()
  155. if err != nil {
  156. log.Fatalf("unable to create session: %s", err)
  157. }
  158. defer session.Close()
  159. // Set up terminal modes
  160. modes := TerminalModes{
  161. ECHO: 0, // disable echoing
  162. TTY_OP_ISPEED: 14400, // input speed = 14.4kbaud
  163. TTY_OP_OSPEED: 14400, // output speed = 14.4kbaud
  164. }
  165. // Request pseudo terminal
  166. if err := session.RequestPty("xterm", 80, 40, modes); err != nil {
  167. log.Fatalf("request for pseudo terminal failed: %s", err)
  168. }
  169. // Start remote shell
  170. if err := session.Shell(); err != nil {
  171. log.Fatalf("failed to start shell: %s", err)
  172. }
  173. }