当前位置: 首页>>代码示例>>Golang>>正文


Golang ssh.ClientAuthKeyring函数代码示例

本文整理汇总了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
}
开发者ID:reth-,项目名称:mole,代码行数:29,代码来源:ssh.go

示例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")
	}
}
开发者ID:kr,项目名称:runx,代码行数:35,代码来源:ssh_test.go

示例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
}
开发者ID:hermanschaaf,项目名称:goship,代码行数:28,代码来源:goship.go

示例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
}
开发者ID:afajl,项目名称:ctrl,代码行数:27,代码来源:connpool.go

示例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()
}
开发者ID:tobert,项目名称:gdsh,代码行数:34,代码来源:connection.go

示例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())
}
开发者ID:Civil,项目名称:gossh,代码行数:32,代码来源:ssh_conn_test.go

示例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)
	}
}
开发者ID:postfix,项目名称:sshPortForward,代码行数:33,代码来源:sshPortForward.go

示例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")
	}))
}
开发者ID:hut8,项目名称:sandhog,代码行数:58,代码来源:sandhog.go

示例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
}
开发者ID:QLeelulu,项目名称:goatherd,代码行数:56,代码来源:handel.go

示例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
}
开发者ID:brianbrunner,项目名称:omg,代码行数:11,代码来源:test_unix_test.go

示例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),
			},
		},
	}
}
开发者ID:rubiojr,项目名称:victor,代码行数:11,代码来源:client.go

示例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
}
开发者ID:AsherBond,项目名称:cf-release,代码行数:15,代码来源:test_unix_test.go

示例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
}
开发者ID:sdvdxl,项目名称:go.crypto,代码行数:15,代码来源:test_unix_test.go

示例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
}
开发者ID:EdevMosaic,项目名称:packer,代码行数:17,代码来源:ssh.go

示例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
}
开发者ID:ramonvanalteren,项目名称:packer,代码行数:16,代码来源:ssh.go


注:本文中的code/google/com/p/go/crypto/ssh.ClientAuthKeyring函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。