78 lines
1.5 KiB
Plaintext
78 lines
1.5 KiB
Plaintext
package main
|
|
|
|
import (
|
|
"log"
|
|
"strings"
|
|
|
|
"github.com/gliderlabs/ssh"
|
|
"golang.org/x/crypto/ssh/terminal"
|
|
)
|
|
|
|
// func main() {
|
|
// ssh.Handle(func(s ssh.Session) {
|
|
// io.WriteString(s, "Hello world\n")
|
|
// })
|
|
|
|
// log.Fatal(ssh.ListenAndServe(":2222", nil, ssh.HostKeyFile("../.ssh/id_rsa")))
|
|
// }
|
|
|
|
// func main() {
|
|
// ssh.ListenAndServe(":2222", nil, ssh.HostKeyFile("../.ssh/id_rsa"),
|
|
// ssh.PasswordAuth(func(ctx ssh.Context, pass string) bool {
|
|
// return pass == "123456"
|
|
// }),
|
|
// )
|
|
// }
|
|
|
|
func main() {
|
|
ssh.Handle(func(s ssh.Session) {
|
|
|
|
term := terminal.NewTerminal(s, "> ")
|
|
for {
|
|
line, err := term.ReadLine()
|
|
if err != nil {
|
|
break
|
|
}
|
|
// 在这里处理输入的命令
|
|
// 您可以根据需要添加更多的逻辑
|
|
if strings.TrimSpace(line) == "exit" {
|
|
break
|
|
}
|
|
|
|
log.Println(line)
|
|
if line != "" {
|
|
term.Write(append([]byte(line), '\n'))
|
|
}
|
|
}
|
|
log.Println("terminal closed")
|
|
})
|
|
|
|
log.Println("starting ssh server on port 2222...")
|
|
ssh.ListenAndServe(":2222", nil, ssh.HostKeyFile("../.ssh/id_rsa"),
|
|
ssh.PasswordAuth(func(ctx ssh.Context, pass string) bool {
|
|
return pass == "123456"
|
|
}),
|
|
)
|
|
}
|
|
|
|
func Handle(sess ssh.Session) {
|
|
term := terminal.NewTerminal(sess, "> ")
|
|
for {
|
|
line, err := term.ReadLine()
|
|
if err != nil {
|
|
break
|
|
}
|
|
// 在这里处理输入的命令
|
|
// 您可以根据需要添加更多的逻辑
|
|
if strings.TrimSpace(line) == "exit" {
|
|
break
|
|
}
|
|
|
|
log.Println(line)
|
|
if line != "" {
|
|
term.Write(append([]byte(line), '\n'))
|
|
}
|
|
}
|
|
log.Println("terminal closed")
|
|
}
|