Last active
March 8, 2025 00:59
-
-
Save yungwarlock/2e1fb9d2a498c7884edc8ae6cb01f549 to your computer and use it in GitHub Desktop.
Simple SSH Server
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module one_dead | |
go 1.19 | |
require ( | |
github.com/gliderlabs/ssh v0.3.8 | |
golang.org/x/crypto v0.31.0 // indirect | |
) | |
require ( | |
github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be // indirect | |
golang.org/x/sys v0.28.0 // indirect | |
golang.org/x/term v0.27.0 | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"io" | |
"log" | |
"golang.org/x/term" | |
"github.com/gliderlabs/ssh" | |
) | |
func main() { | |
ssh.Handle(func(s ssh.Session) { | |
io.WriteString(s, fmt.Sprintf("Hello %s\n", s.User())) | |
term := term.NewTerminal(s, "> ") | |
for { | |
line, err := term.ReadLine() | |
if err != nil { | |
if err == io.EOF { | |
break | |
} | |
io.WriteString(s, fmt.Sprintf("Error: %v\n", err)) | |
continue | |
} | |
switch line { | |
case "up": | |
io.WriteString(s, "I have gone up\n") | |
case "down": | |
io.WriteString(s, "I have gone down\n") | |
case "quit": | |
io.WriteString(s, "Goodbye!\n") | |
return | |
default: | |
io.WriteString(s, fmt.Sprintf("Unknown command: %s\n", line)) | |
} | |
} | |
}) | |
log.Println("starting ssh server on port 2222...") | |
log.Fatal(ssh.ListenAndServe(":2222", nil)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment