Hello everyone! I hope this video has helped solve your questions and issues. This video is shared because a solution has been found for the question/problem. I create videos for questions that have solutions. If you have any other issues, feel free to reach out to me on Instagram: / ky.emrah
Below, you can find the text related to the question/problem. In the video, the question will be presented first, followed by the answers. If the video moves too fast, feel free to pause and review the answers. If you need more detailed information, you can find the necessary sources and links at the bottom of this description. I hope this video has been helpful, and even if it doesn't directly solve your problem, it will guide you to the source of the solution. I'd appreciate it if you like the video and subscribe to my channel!GO SSH Server - How to write stdout/stderr back to ssh client?
I am writing a extremely barebones SSH server (to complement my go SSH client) and I am almost complete, except I ran in to a wall.
After running the command the client sends in its request payload, and getting the StdoutPipe/StderrPipe, it seems that writing those to the channel doesn't send it back to the client (the client just hangs waiting on its session.Run). I know the client works, because all of its commands succeed with a normal OpenSSH server, so I must be doing something wrong in the server code.
Server Code (abridged to the channel handling sections):
func handleChannel(newChannel ssh.NewChannel) {
// Error out channels other than 'session'
if newChannel.ChannelType() != "session" {
logError("SSH channel error", fmt.Errorf("unauthorized channel type requested: %s", newChannel.ChannelType()), false)
return
}
// Accept the channel
channel, requests, err := newChannel.Accept()
if err != nil {
logError("SSH channel error", fmt.Errorf("could not accept channel: %v", err), false)
return
}
defer channel.Close()
fmt.Printf("DEBUG: Accepted new channel (type=%s)\n", newChannel.ChannelType())
// Loop client requests - Only allow SFTP or Exec
for req := range requests {
fmt.Printf(" DEBUG: Received Request (type=%s)\n", req.Type)
switch req.Type {
case "exec":
command, err := StripPayloadHeader(req.Payload)
if err != nil {
logError("SSH request error", fmt.Errorf("exec: failed to strip request payload header: %v", err), false)
continue
}
if req.WantReply {
fmt.Printf(" DEBUG: Sending (reply) confirmation after command request\n")
req.Reply(true, nil)
}
err = executeCommand(channel, command)
if err != nil {
logError("SSH request error", fmt.Errorf("failed command execution: %v", err), false)
continue
}
fmt.Printf(" DEBUG: Sending (reply) confirmation after command execution\n")
req.Reply(true, nil)
case "subsystem":
subsystem, err := StripPayloadHeader(req.Payload)
if err != nil {
logError("SSH request error", fmt.Errorf("subsystem: failed to strip request payload header: %v", err), false)
continue
}
if subsystem != "sftp" {
req.Reply(false, nil)
logError("SSH request error", fmt.Errorf("received unauthorized subsystem %s", subsystem), false)
continue
}
if req.WantReply {
fmt.Printf(" DEBUG: Sending (reply) confirmation after sftp request\n")
req.Reply(true, nil)
}
fmt.Printf(" DEBUG: Starting SFTP server\n")
err = HandleSFTP(channel)
fmt.Printf(" DEBUG: Finished SFTP server\n")
if err != nil {
logError("SSH request error", fmt.Errorf("failed sftp: %v", err), false)
continue
}
fmt.Printf(" DEBUG: Sending (reply) confirmation after sftp completion\n")
req.Reply(true, nil)
default:
req.Reply(false, nil) // Reject unknown requests
}
fmt.Printf(" DEBUG: Finished Request (type=%s)\n", req.Type)
}
// Close thSource of the question:
https://stackoverflow.com/questions/7...
Question and source license information:
https://meta.stackexchange.com/help/l...
https://stackoverflow.com/
Информация по комментариям в разработке