You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

doc.go 1.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. /*
  2. Package ssh wraps the crypto/ssh package with a higher-level API for building
  3. SSH servers. The goal of the API was to make it as simple as using net/http, so
  4. the API is very similar.
  5. You should be able to build any SSH server using only this package, which wraps
  6. relevant types and some functions from crypto/ssh. However, you still need to
  7. use crypto/ssh for building SSH clients.
  8. ListenAndServe starts an SSH server with a given address, handler, and options. The
  9. handler is usually nil, which means to use DefaultHandler. Handle sets DefaultHandler:
  10. ssh.Handle(func(s ssh.Session) {
  11. io.WriteString(s, "Hello world\n")
  12. })
  13. log.Fatal(ssh.ListenAndServe(":2222", nil))
  14. If you don't specify a host key, it will generate one every time. This is convenient
  15. except you'll have to deal with clients being confused that the host key is different.
  16. It's a better idea to generate or point to an existing key on your system:
  17. log.Fatal(ssh.ListenAndServe(":2222", nil, ssh.HostKeyFile("/Users/progrium/.ssh/id_rsa")))
  18. Although all options have functional option helpers, another way to control the
  19. server's behavior is by creating a custom Server:
  20. s := &ssh.Server{
  21. Addr: ":2222",
  22. Handler: sessionHandler,
  23. PublicKeyHandler: authHandler,
  24. }
  25. s.AddHostKey(hostKeySigner)
  26. log.Fatal(s.ListenAndServe())
  27. This package automatically handles basic SSH requests like setting environment
  28. variables, requesting PTY, and changing window size. These requests are
  29. processed, responded to, and any relevant state is updated. This state is then
  30. exposed to you via the Session interface.
  31. The one big feature missing from the Session abstraction is signals. This was
  32. started, but not completed. Pull Requests welcome!
  33. */
  34. package ssh