Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.12 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "fmt"
  5. "io"
  6. "log"
  7. "os"
  8. "os/exec"
  9. "syscall"
  10. "unsafe"
  11.  
  12. "github.com/gliderlabs/ssh"
  13. "github.com/kr/pty"
  14. )
  15.  
  16. var Port = "9003"
  17.  
  18. func setWinsize(f *os.File, w, h int) {
  19. syscall.Syscall(syscall.SYS_IOCTL, f.Fd(), uintptr(syscall.TIOCSWINSZ),
  20. uintptr(unsafe.Pointer(&struct{ h, w, x, y uint16 }{uint16(h), uint16(w), 0, 0})))
  21. }
  22.  
  23. func Run(s ssh.Session) {
  24. cmd := exec.Command("top")
  25. ptyReq, winCh, isPty := s.Pty()
  26. if isPty {
  27. io.WriteString(s, fmt.Sprintf("Welcome, %s\n", s.User()))
  28. cmd.Env = append(cmd.Env, fmt.Sprintf("TERM=%s", ptyReq.Term))
  29. f, err := pty.Start(cmd)
  30. if err != nil {
  31. panic(err)
  32. }
  33. go func() {
  34. for win := range winCh {
  35. setWinsize(f, win.Width, win.Height)
  36. }
  37. }()
  38. go func() {
  39. io.Copy(f, s) //stdin
  40. }()
  41. io.Copy(s, f) //stdout
  42. cmd.Wait()
  43. } else {
  44. io.WriteString(s, "No PTY requested.\n")
  45. s.Exit(1)
  46. }
  47. }
  48.  
  49. func main() {
  50. ssh.Handle(func(s ssh.Session) {
  51. Run(s)
  52. })
  53. log.Println("starting ssh server on Port " + Port)
  54. err := ssh.ListenAndServe(":" + Port, nil)
  55. if err != nil {
  56. log.Fatal(err)
  57. }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement