// Open a new SSH channel sshChannel, err := sshClient.OpenChannel("session", payload) if err != nil { log.Fatal("Failed to open SSH channel: ", err) } // Close the channel gracefully err = sshChannel.Close() if err != nil { log.Fatal("Failed to close SSH channel:", err) }
// Listen to a specific port on the local machine listener, err := net.Listen("tcp", "localhost:8080") if err != nil { log.Fatal("Failed to listen:", err) } // Accept incoming connections for { conn, err := listener.Accept() if err != nil { log.Fatal("Failed to accept connection:", err) } // Open an SSH channel with the remote endpoint sshChannel, requests, err := ssh.NewServerConn(conn, sshConfig) if err != nil { log.Fatal("Failed to open SSH channel:", err) } // Handle incoming requests go handleRequests(requests) // Wait for the channel to close err = sshChannel.Wait() if err != nil { log.Fatal("SSH channel failed with error:", err) } }In both examples, the "Close" function is used to terminate the communication channel in a secure and controlled way.