example_test.go 985 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package ssh_test
  2. import (
  3. "errors"
  4. "io"
  5. "os"
  6. "tailscale.com/tempfork/gliderlabs/ssh"
  7. )
  8. func ExampleListenAndServe() {
  9. ssh.ListenAndServe(":2222", func(s ssh.Session) {
  10. io.WriteString(s, "Hello world\n")
  11. })
  12. }
  13. func ExamplePasswordAuth() {
  14. ssh.ListenAndServe(":2222", nil,
  15. ssh.PasswordAuth(func(ctx ssh.Context, pass string) bool {
  16. return pass == "secret"
  17. }),
  18. )
  19. }
  20. func ExampleNoPty() {
  21. ssh.ListenAndServe(":2222", nil, ssh.NoPty())
  22. }
  23. func ExamplePublicKeyAuth() {
  24. ssh.ListenAndServe(":2222", nil,
  25. ssh.PublicKeyAuth(func(ctx ssh.Context, key ssh.PublicKey) error {
  26. data, err := os.ReadFile("/path/to/allowed/key.pub")
  27. if err != nil {
  28. return err
  29. }
  30. allowed, _, _, _, err := ssh.ParseAuthorizedKey(data)
  31. if err != nil {
  32. return err
  33. }
  34. if !ssh.KeysEqual(key, allowed) {
  35. return errors.New("some error")
  36. }
  37. return nil
  38. }),
  39. )
  40. }
  41. func ExampleHostKeyFile() {
  42. ssh.ListenAndServe(":2222", nil, ssh.HostKeyFile("/path/to/host/key"))
  43. }