woodpecker/vendor/code.google.com/p/go.crypto/ssh/example_test.go

192 lines
5 KiB
Go
Raw Normal View History

2015-05-22 18:37:40 +00:00
// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package ssh
import (
"bytes"
"fmt"
"io/ioutil"
"log"
"net/http"
2015-09-30 01:21:17 +00:00
"code.google.com/p/go.crypto/ssh/terminal"
2015-05-22 18:37:40 +00:00
)
2015-09-30 01:21:17 +00:00
func ExampleListen() {
2015-05-22 18:37:40 +00:00
// An SSH server is represented by a ServerConfig, which holds
// certificate details and handles authentication of ServerConns.
config := &ServerConfig{
2015-09-30 01:21:17 +00:00
PasswordCallback: func(conn *ServerConn, user, pass string) bool {
return user == "testuser" && pass == "tiger"
2015-05-22 18:37:40 +00:00
},
}
privateBytes, err := ioutil.ReadFile("id_rsa")
if err != nil {
panic("Failed to load private key")
}
private, err := ParsePrivateKey(privateBytes)
if err != nil {
panic("Failed to parse private key")
}
config.AddHostKey(private)
// Once a ServerConfig has been configured, connections can be
// accepted.
2015-09-30 01:21:17 +00:00
listener, err := Listen("tcp", "0.0.0.0:2022", config)
2015-05-22 18:37:40 +00:00
if err != nil {
panic("failed to listen for connection")
}
2015-09-30 01:21:17 +00:00
sConn, err := listener.Accept()
2015-05-22 18:37:40 +00:00
if err != nil {
panic("failed to accept incoming connection")
}
2015-09-30 01:21:17 +00:00
if err := sConn.Handshake(); err != nil {
2015-05-22 18:37:40 +00:00
panic("failed to handshake")
}
2015-09-30 01:21:17 +00:00
// A ServerConn multiplexes several channels, which must
// themselves be Accepted.
for {
// Accept reads from the connection, demultiplexes packets
// to their corresponding channels and returns when a new
// channel request is seen. Some goroutine must always be
// calling Accept; otherwise no messages will be forwarded
// to the channels.
channel, err := sConn.Accept()
if err != nil {
panic("error from Accept")
}
2015-05-22 18:37:40 +00:00
// Channels have a type, depending on the application level
// protocol intended. In the case of a shell, the type is
// "session" and ServerShell may be used to present a simple
// terminal interface.
2015-09-30 01:21:17 +00:00
if channel.ChannelType() != "session" {
channel.Reject(UnknownChannelType, "unknown channel type")
2015-05-22 18:37:40 +00:00
continue
}
2015-09-30 01:21:17 +00:00
channel.Accept()
2015-05-22 18:37:40 +00:00
term := terminal.NewTerminal(channel, "> ")
2015-09-30 01:21:17 +00:00
serverTerm := &ServerTerminal{
Term: term,
Channel: channel,
}
2015-05-22 18:37:40 +00:00
go func() {
defer channel.Close()
for {
2015-09-30 01:21:17 +00:00
line, err := serverTerm.ReadLine()
2015-05-22 18:37:40 +00:00
if err != nil {
break
}
fmt.Println(line)
}
}()
}
}
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
2015-09-30 01:21:17 +00:00
// implementation of ClientAuth via the Auth field in ClientConfig.
2015-05-22 18:37:40 +00:00
config := &ClientConfig{
User: "username",
2015-09-30 01:21:17 +00:00
Auth: []ClientAuth{
// ClientAuthPassword wraps a ClientPassword implementation
// in a type that implements ClientAuth.
ClientAuthPassword(password("yourpassword")),
2015-05-22 18:37:40 +00:00
},
}
client, err := 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())
}
2015-09-30 01:21:17 +00:00
func ExampleClientConn_Listen() {
2015-05-22 18:37:40 +00:00
config := &ClientConfig{
User: "username",
2015-09-30 01:21:17 +00:00
Auth: []ClientAuth{
ClientAuthPassword(password("password")),
2015-05-22 18:37:40 +00:00
},
}
// Dial your ssh server.
conn, err := 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 ExampleSession_RequestPty() {
// Create client config
config := &ClientConfig{
User: "username",
2015-09-30 01:21:17 +00:00
Auth: []ClientAuth{
ClientAuthPassword(password("password")),
2015-05-22 18:37:40 +00:00
},
}
// Connect to ssh server
conn, err := 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 := TerminalModes{
ECHO: 0, // disable echoing
TTY_OP_ISPEED: 14400, // input speed = 14.4kbaud
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)
}
}