Skip to content

Instantly share code, notes, and snippets.

@tolgahanakgun
Created June 27, 2025 08:55
Show Gist options
  • Save tolgahanakgun/a8582bd7ac1dbcf0465fa555f6bb764f to your computer and use it in GitHub Desktop.
Save tolgahanakgun/a8582bd7ac1dbcf0465fa555f6bb764f to your computer and use it in GitHub Desktop.
simple telnetd
// This was coded by claude.ai
package main
import (
"bufio"
"fmt"
"net"
"os"
"os/exec"
"strings"
)
var serverPassword string
func main() {
// Get port number from console
fmt.Print("Enter port number (default 2323): ")
portScanner := bufio.NewScanner(os.Stdin)
portScanner.Scan()
port := strings.TrimSpace(portScanner.Text())
if port == "" {
port = "2323"
}
// Get password from console at startup
fmt.Print("Set server password (empty for no auth): ")
passwordScanner := bufio.NewScanner(os.Stdin)
passwordScanner.Scan()
serverPassword = strings.TrimSpace(passwordScanner.Text())
fmt.Println() // New line after password input
listener, err := net.Listen("tcp", ":"+port)
if err != nil {
fmt.Printf("Error starting server: %v\n", err)
return
}
defer listener.Close()
if serverPassword == "" {
fmt.Printf("Telnet server listening on :%s (NO AUTHENTICATION)\n", port)
} else {
fmt.Printf("Telnet server listening on :%s (Password required)\n", port)
}
for {
conn, err := listener.Accept()
if err != nil {
fmt.Printf("Error accepting connection: %v\n", err)
continue
}
go handleConnection(conn)
}
}
func handleConnection(conn net.Conn) {
defer conn.Close()
conn.Write([]byte("Welcome to Telnet Server\r\n"))
// Authentication if password is set
if serverPassword != "" {
conn.Write([]byte("Password: "))
scanner := bufio.NewScanner(conn)
if !scanner.Scan() {
return
}
inputPassword := strings.TrimSpace(scanner.Text())
if inputPassword != serverPassword {
conn.Write([]byte("Access denied\r\n"))
return
}
conn.Write([]byte("Authentication successful\r\n"))
}
conn.Write([]byte("Type 'exit' to quit\r\n\r\n"))
conn.Write([]byte("$ "))
scanner := bufio.NewScanner(conn)
for scanner.Scan() {
command := strings.TrimSpace(scanner.Text())
if command == "exit" || command == "quit" {
conn.Write([]byte("Goodbye!\r\n"))
break
}
if command == "" {
conn.Write([]byte("$ "))
continue
}
// Execute command
cmd := exec.Command("sh", "-c", command)
output, err := cmd.CombinedOutput()
if err != nil {
conn.Write([]byte(fmt.Sprintf("Error: %v\r\n", err)))
} else {
conn.Write(output)
}
conn.Write([]byte("$ "))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment