package main import ( "golang.org/x/crypto/ssh" "log" ) func main() { config := &ssh.ClientConfig{ User: "user", Auth: []ssh.AuthMethod{ ssh.Password("password"), }, } conn, err := ssh.Dial("tcp", "remotehost:22", config) if err != nil { log.Fatalf("failed to dial: %s", err) } session, err := conn.NewSession() if err != nil { log.Fatalf("failed to create session: %s", err) } defer session.Close() reply, err := session.Output("ls -la") if err != nil { log.Fatalf("failed to execute command: %s", err) } log.Println(string(reply)) }
package main import ( "bytes" "golang.org/x/crypto/ssh" "log" ) func main() { config := &ssh.ClientConfig{ User: "user", Auth: []ssh.AuthMethod{ ssh.Password("password"), }, } conn, err := ssh.Dial("tcp", "remotehost:22", config) if err != nil { log.Fatalf("failed to dial: %s", err) } session, err := conn.NewSession() if err != nil { log.Fatalf("failed to create session: %s", err) } defer session.Close() targetFile, err := session.Open("/path/to/remote/file", os.O_RDONLY) if err != nil { log.Fatalf("failed to open file: %s", err) } var buf bytes.Buffer targetFile.WriteTo(&buf) log.Println(buf.String()) }> This example connects to a remote SSH server and opens a file located at `/path/to/remote/file`. It then reads the file content and logs it.