doc.go 3.4 KB

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