func ExampleSession_RequestPty() { // Create client config config := &ssh.ClientConfig{ User: "******", Auth: []ssh.AuthMethod{ ssh.Password("password"), }, } // Connect to ssh server conn, err := ssh.Dial("tcp", "localhost:22", config) if err != nil { log.Fatalf("unable to connect: %s", err) } defer conn.Close() // Create a session session, err := conn.NewSession() if err != nil { log.Fatalf("unable to create session: %s", err) } defer session.Close() // Set up terminal modes modes := ssh.TerminalModes{ ssh.ECHO: 0, // disable echoing ssh.TTY_OP_ISPEED: 14400, // input speed = 14.4kbaud ssh.TTY_OP_OSPEED: 14400, // output speed = 14.4kbaud } // Request pseudo terminal if err := session.RequestPty("xterm", 80, 40, modes); err != nil { log.Fatalf("request for pseudo terminal failed: %s", err) } // Start remote shell if err := session.Shell(); err != nil { log.Fatalf("failed to start shell: %s", err) } }
func ExampleDial() { // An SSH client is represented with a ClientConn. Currently only // the "password" authentication method is supported. // // To authenticate with the remote server you must pass at least one // implementation of AuthMethod via the Auth field in ClientConfig. config := &ssh.ClientConfig{ User: "******", Auth: []ssh.AuthMethod{ ssh.Password("yourpassword"), }, } client, err := ssh.Dial("tcp", "yourserver.com:22", config) if err != nil { panic("Failed to dial: " + err.Error()) } // Each ClientConn can support multiple interactive sessions, // represented by a Session. session, err := client.NewSession() if err != nil { panic("Failed to create session: " + err.Error()) } defer session.Close() // Once a Session is created, you can execute a single command on // the remote side using the Run method. var b bytes.Buffer session.Stdout = &b if err := session.Run("/usr/bin/whoami"); err != nil { panic("Failed to run: " + err.Error()) } fmt.Println(b.String()) }
func ExampleClient_Listen() { config := &ssh.ClientConfig{ User: "******", Auth: []ssh.AuthMethod{ ssh.Password("password"), }, } // Dial your ssh server. conn, err := ssh.Dial("tcp", "localhost:22", config) if err != nil { log.Fatalf("unable to connect: %s", err) } defer conn.Close() // Request the remote side to open port 8080 on all interfaces. l, err := conn.Listen("tcp", "0.0.0.0:8080") if err != nil { log.Fatalf("unable to register tcp forward: %v", err) } defer l.Close() // Serve HTTP with your SSH server acting as a reverse proxy. http.Serve(l, http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) { fmt.Fprintf(resp, "Hello world!\n") })) }
func Tunnel(app string, dbEnvVar string, identity string, port int) error { environ, err := api.VariablesListWithoutAlias(app) if err != nil { return errgo.Mask(err) } dbUrlStr := dbEnvVarValue(dbEnvVar, environ) if dbUrlStr == "" { return errgo.Newf("no such environment variable: %s", dbEnvVar) } dbUrl, err := url.Parse(dbUrlStr) if err != nil { return errgo.Notef(err, "invalid database 'URL': %s", dbUrlStr) } fmt.Printf("Building tunnel to %s\n", dbUrl.Host) var privateKeys []ssh.Signer if identity == "ssh-agent" { var agentConnection io.Closer privateKeys, agentConnection, err = sshkeys.ReadPrivateKeysFromAgent() if err != nil { return errgo.Mask(err) } defer agentConnection.Close() } if len(privateKeys) == 0 { identity = sshkeys.DefaultKeyPath privateKey, err := sshkeys.ReadPrivateKey(identity) if err != nil { return errgo.Mask(err) } privateKeys = append(privateKeys, privateKey) } debug.Println("Identity used:", identity) var client *ssh.Client for _, privateKey := range privateKeys { sshConfig := &ssh.ClientConfig{ User: "******", Auth: []ssh.AuthMethod{ssh.PublicKeys(privateKey)}, } client, err = ssh.Dial("tcp", config.C.SshHost, sshConfig) if err == nil { break } else { config.C.Logger.Println("Fail to connect to the SSH server", err) } } if client == nil { return errgo.Newf("No authentication method has succeeded, please use the flag '-i /path/to/private/key' to specify your private key") } tcpAddr, err := net.ResolveTCPAddr("tcp", fmt.Sprintf("localhost:%d", port)) if err != nil { return errgo.Mask(err) } sock, err := net.ListenTCP("tcp", tcpAddr) if err != nil { return errgo.Mask(err) } defer sock.Close() fmt.Printf("You can access your database on '%v'\n", sock.Addr()) go startIDGenerator() errs := make(chan error) for { select { case err := <-errs: return errgo.Mask(err) default: } connToTunnel, err := sock.Accept() if err != nil { return errgo.Mask(err) } go handleConnToTunnel(client, dbUrl, connToTunnel, errs) } }