本文整理匯總了Golang中code/google/com/p/go/crypto/ssh.ClientAuthKeyring函數的典型用法代碼示例。如果您正苦於以下問題:Golang ClientAuthKeyring函數的具體用法?Golang ClientAuthKeyring怎麽用?Golang ClientAuthKeyring使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了ClientAuthKeyring函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: sshOnConn
func sshOnConn(conn net.Conn, h conf.Host) (*ssh.ClientConn, error) {
var auths []ssh.ClientAuth
if h.Pass != "" {
auths = append(auths, ssh.ClientAuthPassword(password(h.Pass)))
auths = append(auths, ssh.ClientAuthKeyboardInteractive(challenge(h.Pass)))
}
if h.Key != "" {
k := &keyring{}
err := k.loadPEM([]byte(h.Key))
if err != nil {
return nil, err
}
auths = append(auths, ssh.ClientAuthKeyring(k))
}
config := &ssh.ClientConfig{
User: h.User,
Auth: auths,
}
debugln("handshake & authenticate")
client, err := ssh.Client(conn, config)
if err != nil {
return nil, err
}
return client, nil
}
示例2: TestSSHD
func TestSSHD(t *testing.T) {
block, _ := pem.Decode([]byte(testClientPrivateKey))
rsakey, _ := x509.ParsePKCS1PrivateKey(block.Bytes)
pub, _ := ssh.NewPublicKey(&rsakey.PublicKey)
cmd, c, err := startSSHD(ssh.MarshalAuthorizedKey(pub))
if err != nil {
t.Fatal(err)
}
defer cmd.Wait()
defer cmd.Process.Kill()
u, err := user.Current()
if err != nil {
t.Fatal(err)
}
_ = u
config := &ssh.ClientConfig{
User: u.Username,
Auth: []ssh.ClientAuth{ssh.ClientAuthKeyring(&keyring{rsakey})},
}
client, err := ssh.Client(c, config)
if err != nil {
t.Fatal(err)
}
sess, err := client.NewSession()
if err != nil {
t.Fatal(err)
}
out, err := sess.Output("echo hello")
if err != nil {
t.Fatal(err)
}
if string(out) != "hello\n" {
t.Fatalf("out = %q want %q", string(out), "hello\n")
}
}
示例3: remoteCmdOutput
func remoteCmdOutput(username, hostname, privateKey, cmd string) []byte {
block, _ := pem.Decode([]byte(privateKey))
rsakey, _ := x509.ParsePKCS1PrivateKey(block.Bytes)
clientKey := &keychain{rsakey}
clientConfig := &ssh.ClientConfig{
User: username,
Auth: []ssh.ClientAuth{
ssh.ClientAuthKeyring(clientKey),
},
}
client, err := ssh.Dial("tcp", hostname, clientConfig)
if err != nil {
log.Println("ERROR: Failed to dial: " + err.Error())
return []byte{}
}
session, err := client.NewSession()
if err != nil {
log.Println("ERROR: Failed to create session: " + err.Error())
return []byte{}
}
defer session.Close()
output, err := session.Output(cmd)
if err != nil {
log.Printf("ERROR: Failed to run cmd on host %s: %s", hostname, err.Error())
return []byte{}
}
return output
}
示例4: getConn
func getConn(host *host.Host) (*ssh.ClientConn, error) {
hostkey := host.Id
if con, ok := conns[hostkey]; ok {
return con, nil
}
if host.User == "" {
return nil, fmt.Errorf("user not set")
}
for _, keyfile := range host.Keyfiles {
// TODO add key to global keyring, ok?
if err := keys.loadPEM(keyfile); err != nil {
return nil, fmt.Errorf("unable to load %s: %v", keyfile, err)
}
}
config := &ssh.ClientConfig{
User: host.User,
Auth: []ssh.ClientAuth{
ssh.ClientAuthKeyring(keys),
},
}
conn, err := ssh.Dial("tcp", host.ConnStr(), config)
if err != nil {
return nil, fmt.Errorf("unable to connect to %s: %v", host, err)
}
conns[hostkey] = conn
return conn, nil
}
示例5: Connect
func (conn *Conn) Connect() error {
var auth []ssh.ClientAuth
// only load a private key if requested ~/.ssh/id_rsa is _not_ loaded automatically
// ssh-agent should be the usual path
if conn.Key != "" {
kr := new(keyring)
if err := kr.loadPEM(conn.Key); err != nil {
log.Fatal("Couldn't load specified private key '", conn.Key, "': ", err)
}
auth = append(auth, ssh.ClientAuthKeyring(kr))
}
agentSock := os.Getenv("SSH_AUTH_SOCK")
// ssh-agent support, might need to reuse this in the future?
// how bad are 100's or 1000's of connections to the agent?
if agentSock != "" {
sock, err := net.Dial("unix", agentSock)
if err != nil {
log.Fatal("Could not connect to SSH_AUTH_SOCK. Is ssh-agent running?")
}
agent := ssh.NewAgentClient(sock)
auth = append(auth, ssh.ClientAuthAgent(agent))
}
conn.config = &ssh.ClientConfig{
User: conn.User,
Auth: auth,
}
return conn.connect()
}
示例6: TestSshCmd
func TestSshCmd(t *testing.T) {
kc := new(keychain)
kc.load()
config := &ssh.ClientConfig{
User: os.Getenv("USER"),
Auth: []ssh.ClientAuth{
ssh.ClientAuthKeyring(kc),
},
}
client, err := ssh.Dial("tcp", "localhost: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())
}
log.Printf("Result of running whoami via ssh: %s\n", b)
//fmt.Println(b.String())
}
示例7: ConnectAndForward
func ConnectAndForward(addresses Addresses) {
// Load id_rsa file
keychain := new(keyChain)
err := keychain.loadPEM(addresses.PrivateKeyPathString)
if err != nil {
log.Fatalf("Cannot load key: %v", err)
}
// Setup SSH config (type *ssh.ClientConfig)
config := &ssh.ClientConfig{
User: addresses.SSHUserString,
Auth: []ssh.ClientAuth{
ssh.ClientAuthKeyring(keychain),
},
}
// Setup localListener (type net.Listener)
localListener, err := net.Listen("tcp", addresses.LocalAddrString)
if err != nil {
log.Fatalf("net.Listen failed: %v", err)
}
defer localListener.Close()
for {
// Setup localConn (type net.Conn)
localConn, err := localListener.Accept()
if err != nil {
log.Fatalf("listen.Accept failed: %v", err)
}
defer localConn.Close()
go forward(localConn, config, addresses.ServerAddrString, addresses.RemoteAddrString)
}
}
示例8: main
func main() {
logger = log.New(os.Stdout, "wam: ", log.LstdFlags|log.Lshortfile)
logger.Println("sandhog")
configData, err := loadConfig()
if err != nil {
printUsage()
logger.Fatalln(err)
}
keyring, err := LoadKeyring(configData.keyPath)
if err != nil {
logger.Fatalln(err)
}
logger.Printf("loaded keyring: %s", keyring)
sshConfig := &ssh.ClientConfig{
User: "wam",
Auth: []ssh.ClientAuth{
ssh.ClientAuthKeyring(keyring),
},
}
logger.Printf("created SSH client config: %s", sshConfig)
// Dial your ssh server.
logger.Println("connecting")
conn, err := ssh.Dial("tcp", "localhost:22", sshConfig)
if err != nil {
logger.Fatalf("unable to connect: %s\n", err)
}
defer conn.Close()
logger.Println("connected!")
// Request the remote side to open port 8080 on all interfaces.
// When they
remoteListenEndpoint := fmt.Sprintf("127.0.0.1:%d", configData.remotePort)
logger.Printf("requesting remote host listen on: %s\n", remoteListenEndpoint)
listener, err := conn.Listen("tcp", remoteListenEndpoint)
if err != nil {
log.Fatalf("unable to register tcp forward: %s", err)
}
defer listener.Close()
logger.Printf("remote host listening on %s\n", remoteListenEndpoint)
for {
conn, err := listener.Accept()
if err != nil {
logger.Println(err)
break
}
go handleConn(conn, *configData)
}
// Serve HTTP with your SSH server acting as a reverse proxy.
http.Serve(listener, http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
fmt.Fprintf(resp, "Hello world!\n")
}))
}
示例9: check
func (this *Handel) check() (err error) {
if this.auth == nil {
keys := new(keychain)
if err = keys.LoadPEM(this.PrivateKey); err != nil {
return
}
this.auth = &ssh.ClientConfig{
User: this.User,
Auth: []ssh.ClientAuth{
ssh.ClientAuthKeyring(keys),
},
}
}
if this.Port == "" {
this.Port = SSH_PORT
}
if this.client == nil {
this.client, err = ssh.Dial("tcp", this.Host+":"+this.Port, this.auth)
if err != nil {
return
}
}
if this.session == nil {
this.session, err = this.client.NewSession()
if err != nil {
return
}
}
//stdin
if this.FileIn == "" {
this.session.Stdin = os.Stdin
} else if fd, err := os.Open(this.FileIn); err == nil {
this.session.Stdin = fd
} else {
return errors.New("process config error : bad input file : " + err.Error())
}
//stdout
if this.FileOut == "" {
this.session.Stdout = os.Stdout
} else if fd, err := os.OpenFile(this.FileOut, os.O_WRONLY|os.O_CREATE, 0666); err == nil {
this.session.Stdout = fd
} else {
return errors.New("process config error : bad output file : " + err.Error())
}
//stderr
if this.FileErr == "" {
this.session.Stderr = os.Stderr
} else if fd, err := os.OpenFile(this.FileErr, os.O_WRONLY|os.O_CREATE, 0666); err == nil {
this.session.Stderr = fd
} else {
return errors.New("process config error : bad error file : " + err.Error())
}
return
}
示例10: clientConfig
func clientConfig() *ssh.ClientConfig {
kc := new(keychain)
kc.keys = append(kc.keys, rsakey)
config := &ssh.ClientConfig{
User: username(),
Auth: []ssh.ClientAuth{
ssh.ClientAuthKeyring(kc),
},
}
return config
}
示例11: NewClient
func NewClient(user string, addr string, keys *Keychain) *Client {
return &Client{
Addr: addr,
Config: &ssh.ClientConfig{
User: user,
Auth: []ssh.ClientAuth{
ssh.ClientAuthKeyring(keys),
},
},
}
}
示例12: clientConfig
func clientConfig() *ssh.ClientConfig {
keyChecker := storedHostKey{}
keyChecker.Add("ssh-rsa", serializedHostKey)
kc := new(keychain)
kc.keys = append(kc.keys, rsakey)
config := &ssh.ClientConfig{
User: username(),
Auth: []ssh.ClientAuth{
ssh.ClientAuthKeyring(kc),
},
HostKeyChecker: &keyChecker,
}
return config
}
示例13: clientConfig
func clientConfig() *ssh.ClientConfig {
keyChecker := storedHostKey{}
keyChecker.Add(hostKey.PublicKey())
kc := new(keychain)
kc.keys = append(kc.keys, privateKey)
config := &ssh.ClientConfig{
User: username(),
Auth: []ssh.ClientAuth{
ssh.ClientAuthKeyring(kc),
},
HostKeyChecker: &keyChecker,
}
return config
}
示例14: sshConfig
// sshConfig returns the ssh configuration.
func sshConfig(state multistep.StateBag) (*gossh.ClientConfig, error) {
config := state.Get("config").(*Config)
privateKey := state.Get("ssh_private_key").(string)
keyring := new(ssh.SimpleKeychain)
if err := keyring.AddPEMKey(privateKey); err != nil {
return nil, fmt.Errorf("Error setting up SSH config: %s", err)
}
sshConfig := &gossh.ClientConfig{
User: config.SSHUsername,
Auth: []gossh.ClientAuth{gossh.ClientAuthKeyring(keyring)},
}
return sshConfig, nil
}
示例15: sshConfig
func sshConfig(state map[string]interface{}) (*gossh.ClientConfig, error) {
config := state["config"].(config)
privateKey := state["privateKey"].(string)
keyring := new(ssh.SimpleKeychain)
if err := keyring.AddPEMKey(privateKey); err != nil {
return nil, fmt.Errorf("Error setting up SSH config: %s", err)
}
return &gossh.ClientConfig{
User: config.SSHUsername,
Auth: []gossh.ClientAuth{
gossh.ClientAuthKeyring(keyring),
},
}, nil
}