通过SSH使用tcell+tview的例子

367 阅读1分钟

tview-ssh

Example using tcell+tview over SSH using gliderlabs/ssh

使用gliderlabs/ssh在SSH上使用tcell+tview的例子,无需分配PTY或创建子进程。

有一点胶水,但对于一个库来说可能还不够?另外,它可能是不完整的。下面是做一个SSH服务器的样子,当你连接时显示一个模式。

func main() {
	ssh.Handle(func(sess ssh.Session) {
		screen, err := NewSessionScreen(sess)
		if err != nil {
			fmt.Fprintln(sess.Stderr(), "unable to create screen:", err)
			return
		}

		// tview says we don't have to do this
		// when using SetScreen, but it lies
		if err := screen.Init(); err != nil {
			fmt.Fprintln(sess.Stderr(), "unable to init screen:", err)
			return
		}

		app := tview.NewApplication().SetScreen(screen).EnableMouse(true)

		modal := tview.NewModal().
			SetText("Do you want to quit the application?").
			AddButtons([]string{"Quit", "Cancel"}).
			SetDoneFunc(func(buttonIndex int, buttonLabel string) {
				if buttonLabel == "Quit" {
					app.Stop()
				}
			})

		app.SetRoot(modal, false)
		if err := app.Run(); err != nil {
			fmt.Fprintln(sess.Stderr(), err)
			return
		}

		sess.Exit(0)
	})

	log.Fatal(ssh.ListenAndServe(":2222", nil, ssh.HostKeyFile("/Users/progrium/.ssh/id_rsa")))
}

如果你尝试这个,把hostkey文件改成适合你的东西。