example_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. "exp/terminal"
  8. "fmt"
  9. "io/ioutil"
  10. )
  11. func ExampleListen() {
  12. // An SSH server is represented by a ServerConfig, which holds
  13. // certificate details and handles authentication of ServerConns.
  14. config := &ServerConfig{
  15. PasswordCallback: func(conn *ServerConn, user, pass string) bool {
  16. return user == "testuser" && pass == "tiger"
  17. },
  18. }
  19. pemBytes, err := ioutil.ReadFile("id_rsa")
  20. if err != nil {
  21. panic("Failed to load private key")
  22. }
  23. if err = config.SetRSAPrivateKey(pemBytes); err != nil {
  24. panic("Failed to parse private key")
  25. }
  26. // Once a ServerConfig has been configured, connections can be
  27. // accepted.
  28. listener, err := Listen("tcp", "0.0.0.0:2022", config)
  29. if err != nil {
  30. panic("failed to listen for connection")
  31. }
  32. sConn, err := listener.Accept()
  33. if err != nil {
  34. panic("failed to accept incoming connection")
  35. }
  36. if err := sConn.Handshake(); err != nil {
  37. panic("failed to handshake")
  38. }
  39. // A ServerConn multiplexes several channels, which must
  40. // themselves be Accepted.
  41. for {
  42. // Accept reads from the connection, demultiplexes packets
  43. // to their corresponding channels and returns when a new
  44. // channel request is seen. Some goroutine must always be
  45. // calling Accept; otherwise no messages will be forwarded
  46. // to the channels.
  47. channel, err := sConn.Accept()
  48. if err != nil {
  49. panic("error from Accept")
  50. }
  51. // Channels have a type, depending on the application level
  52. // protocol intended. In the case of a shell, the type is
  53. // "session" and ServerShell may be used to present a simple
  54. // terminal interface.
  55. if channel.ChannelType() != "session" {
  56. channel.Reject(UnknownChannelType, "unknown channel type")
  57. continue
  58. }
  59. channel.Accept()
  60. term := terminal.NewTerminal(channel, "> ")
  61. serverTerm := &ServerTerminal{
  62. Term: term,
  63. Channel: channel,
  64. }
  65. go func() {
  66. defer channel.Close()
  67. for {
  68. line, err := serverTerm.ReadLine()
  69. if err != nil {
  70. break
  71. }
  72. fmt.Println(line)
  73. }
  74. }()
  75. }
  76. }
  77. func ExampleDial() {
  78. // An SSH client is represented with a ClientConn. Currently only
  79. // the "password" authentication method is supported.
  80. //
  81. // To authenticate with the remote server you must pass at least one
  82. // implementation of ClientAuth via the Auth field in ClientConfig.
  83. config := &ClientConfig{
  84. User: "username",
  85. Auth: []ClientAuth{
  86. // ClientAuthPassword wraps a ClientPassword implementation
  87. // in a type that implements ClientAuth.
  88. ClientAuthPassword(password("yourpassword")),
  89. },
  90. }
  91. client, err := Dial("tcp", "yourserver.com:22", config)
  92. if err != nil {
  93. panic("Failed to dial: " + err.Error())
  94. }
  95. // Each ClientConn can support multiple interactive sessions,
  96. // represented by a Session.
  97. session, err := client.NewSession()
  98. if err != nil {
  99. panic("Failed to create session: " + err.Error())
  100. }
  101. defer session.Close()
  102. // Once a Session is created, you can execute a single command on
  103. // the remote side using the Run method.
  104. var b bytes.Buffer
  105. session.Stdout = &b
  106. if err := session.Run("/usr/bin/whoami"); err != nil {
  107. panic("Failed to run: " + err.Error())
  108. }
  109. fmt.Println(b.String())
  110. }