Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "fmt"
- "io"
- "log"
- "os"
- "os/exec"
- "syscall"
- "unsafe"
- "github.com/gliderlabs/ssh"
- "github.com/kr/pty"
- )
- var Port = "9003"
- func setWinsize(f *os.File, w, h int) {
- syscall.Syscall(syscall.SYS_IOCTL, f.Fd(), uintptr(syscall.TIOCSWINSZ),
- uintptr(unsafe.Pointer(&struct{ h, w, x, y uint16 }{uint16(h), uint16(w), 0, 0})))
- }
- func Run(s ssh.Session) {
- cmd := exec.Command("top")
- ptyReq, winCh, isPty := s.Pty()
- if isPty {
- io.WriteString(s, fmt.Sprintf("Welcome, %s\n", s.User()))
- cmd.Env = append(cmd.Env, fmt.Sprintf("TERM=%s", ptyReq.Term))
- f, err := pty.Start(cmd)
- if err != nil {
- panic(err)
- }
- go func() {
- for win := range winCh {
- setWinsize(f, win.Width, win.Height)
- }
- }()
- go func() {
- io.Copy(f, s) //stdin
- }()
- io.Copy(s, f) //stdout
- cmd.Wait()
- } else {
- io.WriteString(s, "No PTY requested.\n")
- s.Exit(1)
- }
- }
- func main() {
- ssh.Handle(func(s ssh.Session) {
- Run(s)
- })
- log.Println("starting ssh server on Port " + Port)
- err := ssh.ListenAndServe(":" + Port, nil)
- if err != nil {
- log.Fatal(err)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement