• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Golang agent.NewClient函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Golang中golang.org/x/crypto/ssh/agent.NewClient函数的典型用法代码示例。如果您正苦于以下问题:Golang NewClient函数的具体用法?Golang NewClient怎么用?Golang NewClient使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了NewClient函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。

示例1: ListKeys

// ListKeys connects to the local SSH Agent and lists all the public keys
// loaded into it. It returns user friendly error message when it has trouble.
func ListKeys() ([]*agent.Key, error) {
	sshAuthSock := os.Getenv("SSH_AUTH_SOCK")
	if sshAuthSock == "" {
		return nil, fmt.Errorf(
			"The SSH_AUTH_SOCK environment variable is not set, which normally\n" +
				"means that no SSH Agent is running.")
	}

	conn, err := net.Dial("unix", sshAuthSock)
	if err != nil {
		return nil, fmt.Errorf(
			"Error connecting to agent: %s\n\n"+
				"The agent address is detected using the SSH_AUTH_SOCK environment\n"+
				"variable. Please verify this variable is correct and the SSH agent\n"+
				"is properly set up.",
			err)
	}
	defer conn.Close()

	agent := agent.NewClient(conn)
	loadedKeys, err := agent.List()
	if err != nil {
		return nil, fmt.Errorf("Error listing keys: %s", err)
	}
	return loadedKeys, err
}
开发者ID:mbrodala,项目名称:otto,代码行数:28,代码来源:sshagent.go


示例2: connect

func (this *SSHExecutor) connect() error {
	// auths holds the detected ssh auth methods
	auths := []ssh.AuthMethod{}

	// figure out what auths are requested, what is supported
	if this.Password != "" {
		auths = append(auths, ssh.Password(this.Password))
	}

	if sshAgent, err := net.Dial("unix", os.Getenv("SSH_AUTH_SOCK")); err == nil {
		auths = append(auths, ssh.PublicKeysCallback(agent.NewClient(sshAgent).Signers))
		defer sshAgent.Close()
	}

	config := &ssh.ClientConfig{
		User: this.User,
		Auth: auths,
	}

	client, err := ssh.Dial("tcp", this.Hostname+":"+strconv.Itoa(this.Port), config)
	if err != nil {
		return err
	}

	session, err := client.NewSession()
	if err != nil {
		return err
	}

	this.Session = session

	return nil
}
开发者ID:vence722,项目名称:ssh_tool,代码行数:33,代码来源:executor.go


示例3: Client

func (conf *SshConfig) Client() (*ssh.Client, error) {

	auths := []ssh.AuthMethod{}

	if !Empty(conf.Password) {
		auths = append(auths, ssh.Password(conf.Password))
	}

	if sshAgent, err := net.Dial("unix", os.Getenv("SSH_AUTH_SOCK")); err == nil {
		auths = append(auths, ssh.PublicKeysCallback(agent.NewClient(sshAgent).Signers))
		defer sshAgent.Close()
	}

	if pubkey, err := getKeyFile(conf.Key); err == nil {
		auths = append(auths, ssh.PublicKeys(pubkey))
	}

	config := &ssh.ClientConfig{
		User: conf.User,
		Auth: auths,
	}

	client, err := ssh.Dial("tcp", conf.Name+":"+conf.Port, config)
	if err != nil {
		return nil, err
	}

	return client, nil
}
开发者ID:kbdhero,项目名称:cogs,代码行数:29,代码来源:base.go


示例4: connect

// connects to remote server using ClientSSH struct and returns *ssh.Session
func (ssh_conf *ClientSSH) connect() (*ssh.Session, error) {
	// auths holds the detected ssh auth methods
	auths := []ssh.AuthMethod{}

	// figure out what auths are requested, what is supported
	if ssh_conf.Password != "" {
		auths = append(auths, ssh.Password(ssh_conf.Password))
	}

	if sshAgent, err := net.Dial("unix", os.Getenv("SSH_AUTH_SOCK")); err == nil {
		auths = append(auths, ssh.PublicKeysCallback(agent.NewClient(sshAgent).Signers))
		defer sshAgent.Close()
	}

	if pubkey, err := getKeyFile(ssh_conf.Key); err == nil {
		auths = append(auths, ssh.PublicKeys(pubkey))
	}

	config := &ssh.ClientConfig{
		User: ssh_conf.User,
		Auth: auths,
	}

	client, err := ssh.Dial("tcp", ssh_conf.Server+":"+ssh_conf.Port, config)
	if err != nil {
		return nil, err
	}

	session, err := client.NewSession()
	if err != nil {
		return nil, err
	}

	return session, nil
}
开发者ID:rootless4real,项目名称:musicsaur,代码行数:36,代码来源:ssh.go


示例5: initAuthMethod

// initAuthMethod initiates SSH authentication method.
func initAuthMethod() {
	var signers []ssh.Signer

	// If there's a running SSH Agent, try to use its Private keys.
	sock, err := net.Dial("unix", os.Getenv("SSH_AUTH_SOCK"))
	if err == nil {
		agent := agent.NewClient(sock)
		signers, _ = agent.Signers()
	}

	// Try to read user's SSH private keys form the standard paths.
	files := []string{
		os.Getenv("HOME") + "/.ssh/id_rsa",
		os.Getenv("HOME") + "/.ssh/id_dsa",
	}
	for _, file := range files {
		data, err := ioutil.ReadFile(file)
		if err != nil {
			continue
		}
		signer, err := ssh.ParsePrivateKey(data)
		if err != nil {
			continue
		}
		signers = append(signers, signer)

	}
	authMethod = ssh.PublicKeys(signers...)
}
开发者ID:roblillack,项目名称:sup,代码行数:30,代码来源:ssh.go


示例6: Connection

func Connection(info ConnetctionInfo) *ssh.Client {
	var auths []ssh.AuthMethod
	auths = append(auths, privateKey...)
	if aconn, err := net.Dial("unix", os.Getenv("SSH_AUTH_SOCK")); err == nil {
		auths = append(auths, ssh.PublicKeysCallback(agent.NewClient(aconn).Signers))
	}
	auths = append(auths, ssh.Password(info.Passwd))
	config := ssh.ClientConfig{
		User: info.User,
		Auth: auths,
	}
	for i := 0; i < 3; i++ {
		//conn, err := ssh.Dial("tcp", fmt.Sprintf("%s:%s", info.IP, info.Port), &config)
		conn, err := ssh.DialTimeOut("tcp", fmt.Sprintf("%s:%s", info.IP, info.Port), 30, &config)
		if err == nil {
			return conn
		}
		if i == 2 && err != nil {
			ErrorList = append(ErrorList, fmt.Sprintf("连接%s失败:%s\n", info.IP, err))
			return nil
		}
		time.Sleep(1e9)
	}
	return nil
}
开发者ID:czxichen,项目名称:Goprograme,代码行数:25,代码来源:connectin.go


示例7: main

func main() {
	auth_socket := os.Getenv("SSH_AUTH_SOCK")
	if auth_socket == "" {
		log.Fatal(errors.New("no $SSH_AUTH_SOCK defined"))
	}
	conn, err := net.Dial("unix", auth_socket)
	if err != nil {
		log.Fatal(err)
	}
	defer conn.Close()
	ag := agent.NewClient(conn)
	auths := []ssh.AuthMethod{ssh.PublicKeysCallback(ag.Signers)}

	config := &ssh.ClientConfig{
		User:            Username,
		Auth:            auths,
		HostKeyCallback: KeyScanCallback,
	}

	var wg sync.WaitGroup
	go out(&wg)
	reader := bufio.NewReader(os.Stdin)
	for {
		server, err := reader.ReadString('\n')
		if err == io.EOF {
			break
		}
		server = server[:len(server)-1] // chomp
		wg.Add(2)                       // dial and print
		go dial(server, config, &wg)
	}
	wg.Wait()
}
开发者ID:szuecs,项目名称:go-ssh-keyscan,代码行数:33,代码来源:go-ssh-keyscan.go


示例8: sshClient

func sshClient(username, host string) (*ssh.Client, error) {
	agentconn, err := net.Dial("unix", os.Getenv("SSH_AUTH_SOCK"))
	if err != nil {
		return nil, err
	}

	aclient := agent.NewClient(agentconn)

	conf := &ssh.ClientConfig{
		User: username,
		Auth: []ssh.AuthMethod{
			ssh.PublicKeysCallback(aclient.Signers),
		},
	}

	if strings.Index(host, ":") < 0 {
		host = host + ":22"
	}

	cli, err := ssh.Dial("tcp", host, conf)
	if err != nil {
		return nil, err
	}

	return cli, nil
}
开发者ID:cmikk,项目名称:sshttp,代码行数:26,代码来源:ssh.go


示例9: DialSSH

func DialSSH(addr string, username string, keyFilename string) (conn *SSHConn, err error) {
	conn = &SSHConn{}

	if keyFilename != "" {
		pemKey, err := ioutil.ReadFile(keyFilename)
		if err != nil {
			return
		}

		signer, err := ssh.ParsePrivateKey(pemKey)
		if err != nil {
			return
		}

		conn.Auth = ssh.PublicKeys(signer)
	} else {
		sshAgent, err := net.Dial("unix", os.Getenv("SSH_AUTH_SOCK"))
		if err != nil {
			return
		}

		conn.Auth = ssh.PublicKeysCallback(agent.NewClient(sshAgent).Signers)
	}

	conn.Config = &ssh.ClientConfig{
		User: username,
		Auth: []ssh.AuthMethod{
			conn.Auth,
		},
	}

	conn.Client, err = ssh.Dial("tcp", fmt.Sprintf("%s:22", addr), conn.Config)
	return
}
开发者ID:rgeorgiev583,项目名称:gonfs,代码行数:34,代码来源:dial.go


示例10: New

// New returns a new agent.Agent and the (custom) connection it uses
// to communicate with a running pagent.exe instance (see README.md)
func New() (agent.Agent, net.Conn, error) {
	if !Available() {
		return nil, nil, errors.New("SSH agent requested but Pageant not running")
	}

	return agent.NewClient(&conn{}), nil, nil
}
开发者ID:Zordrak,项目名称:terraform,代码行数:9,代码来源:sshagent_windows.go


示例11: ExampleClientAgent

func ExampleClientAgent() {
	// ssh-agent has a UNIX socket under $SSH_AUTH_SOCK
	socket := os.Getenv("SSH_AUTH_SOCK")
	conn, err := net.Dial("unix", socket)
	if err != nil {
		log.Fatalf("net.Dial: %v", err)
	}
	agentClient := agent.NewClient(conn)
	config := &ssh.ClientConfig{
		User: "username",
		Auth: []ssh.AuthMethod{
			// Use a callback rather than PublicKeys
			// so we only consult the agent once the remote server
			// wants it.
			ssh.PublicKeysCallback(agentClient.Signers),
		},
	}

	sshc, err := ssh.Dial("tcp", "localhost:22", config)
	if err != nil {
		log.Fatalf("Dial: %v", err)
	}
	// .. use sshc
	sshc.Close()
}
开发者ID:Rudloff,项目名称:platform,代码行数:25,代码来源:example_test.go


示例12: SSHAgent

func SSHAgent() ssh.AuthMethod {
	if sshAgent, err := net.Dial("unix", os.Getenv("SSH_AUTH_SOCK")); err == nil {
		return ssh.PublicKeysCallback(agent.NewClient(sshAgent).Signers)
	}

	return nil
}
开发者ID:ebenoist,项目名称:ply,代码行数:7,代码来源:ssh.go


示例13: NewSshExecWithAuth

func NewSshExecWithAuth(user string) *SshExec {

	sshexec := &SshExec{}

	authSocket := os.Getenv("SSH_AUTH_SOCK")
	if authSocket == "" {
		log.Fatal("SSH_AUTH_SOCK required, check that your ssh agent is running")
		return nil
	}

	agentUnixSock, err := net.Dial("unix", authSocket)
	if err != nil {
		log.Fatal(err)
		return nil
	}

	agent := agent.NewClient(agentUnixSock)
	signers, err := agent.Signers()
	if err != nil {
		log.Fatal(err)
		return nil
	}

	sshexec.clientConfig = &ssh.ClientConfig{
		User: user,
		Auth: []ssh.AuthMethod{ssh.PublicKeys(signers...)},
	}

	return sshexec
}
开发者ID:kshlm,项目名称:heketi,代码行数:30,代码来源:ssh.go


示例14: Connect

func (sfs *SftpFileSystem) Connect() error {
	var auths []ssh.AuthMethod
	if aconn, err := net.Dial("unix", os.Getenv("SSH_AUTH_SOCK")); err == nil {
		auths = append(auths, ssh.PublicKeysCallback(agent.NewClient(aconn).Signers))
	}
	if len(sfs.sp.Password) != 0 {
		auths = append(auths, ssh.Password(sfs.sp.Password))
	}

	config := ssh.ClientConfig{
		User: sfs.sp.Username,
		Auth: auths,
	}
	var err error
	sfs.sshClient, err = ssh.Dial("tcp", sfs.sp.Server, &config)
	if err != nil {
		log.Println("unable to connect to [%s]: %v", sfs.sp.Server, err)
		return err
	}

	sfs.sftpClient, err = sftp.NewClient(sfs.sshClient)
	if err != nil {
		log.Println("unable to start sftp subsytem: %v", err)
		return err
	}
	return nil
}
开发者ID:peernode,项目名称:Taipei-Torrent,代码行数:27,代码来源:sftp.go


示例15: reconnect

func (ctx *ExecContext) reconnect() (err error) {
	if ctx.hostname != "" {
		ctx.isReconnecting = true
		username := ctx.username
		addr := fmt.Sprintf("%s:%d", ctx.hostname, ctx.port)
		ctx.unlock()
		agentConn, err := net.Dial("unix", os.Getenv("SSH_AUTH_SOCK"))
		if err != nil {
			ctx.lock()
			ctx.isReconnecting = false
			return err
		}
		defer agentConn.Close()
		ag := agent.NewClient(agentConn)
		auths := []ssh.AuthMethod{ssh.PublicKeysCallback(ag.Signers)}
		config := &ssh.ClientConfig{
			User: username,
			Auth: auths,
		}
		conn, err := net.DialTimeout("tcp", addr, networkTimeout)
		if err != nil {
			ctx.lock()
			ctx.isReconnecting = false
			return err
		}

		timeoutConn := &Conn{conn, networkTimeout, networkTimeout}
		c, chans, reqs, err := ssh.NewClientConn(timeoutConn, addr, config)
		if err != nil {
			ctx.lock()
			ctx.isReconnecting = false
			return err
		}
		client := ssh.NewClient(c, chans, reqs)

		// Send periodic keepalive messages
		go func() {
			t := time.NewTicker(networkTimeout / 2)
			defer t.Stop()
			for {
				<-t.C
				_, _, err := client.Conn.SendRequest("[email protected]", true, nil)
				if err != nil {
					ctx.lock()
					if ctx.sshClient == client {
						ctx.isConnected = false
					}
					ctx.unlock()
					return
				}
			}
		}()
		ctx.lock()
		ctx.isReconnecting = false
		ctx.sshClient = client
	}
	ctx.isConnected = true
	return nil
}
开发者ID:tillberg,项目名称:bismuth,代码行数:59,代码来源:bismuth.go


示例16: sshAgent

func sshAgent() (ssh.AuthMethod, func(), error) {
	sshAgent, err := net.Dial("unix", os.Getenv("SSH_AUTH_SOCK"))
	if err != nil {
		return nil, func() {}, err
	}
	method := ssh.PublicKeysCallback(agent.NewClient(sshAgent).Signers)
	return method, func() { _ = sshAgent.Close() }, nil
}
开发者ID:aybabtme,项目名称:multisshtail,代码行数:8,代码来源:main.go


示例17: main

func main() {
	if !(len(os.Args) > 1) {
		log.Fatal("action required.")
	}
	action := os.Args[1]
	os.Args = append(os.Args[:1], os.Args[2:]...)

	flag.Parse()

	authSocket := os.Getenv("SSH_AUTH_SOCK")
	if authSocket == "" {
		log.Fatal("SSH_AUTH_SOCK required, check that your ssh agent is running")
	}

	agentUnixSock, err := net.Dial("unix", authSocket)
	if err != nil {
		log.Fatal(err)
	}

	agent := agent.NewClient(agentUnixSock)
	signers, err := agent.Signers()
	if err != nil {
		log.Fatal(err)
	}

	clientConfig = &ssh.ClientConfig{
		User: sshUser,
		Auth: []ssh.AuthMethod{ssh.PublicKeys(signers...)},
	}

	hostsList := strings.Split(hosts, ",")
	if !(len(hostsList) > 0) {
		log.Fatal("one or more hosts required, use the -host flag")
	}

	stderrChan = make(chan string, 0)
	stdoutChan = make(chan string, 0)
	go consoleWriter()

	switch action {
	case "deploy":
		if etcdBinary != "" {
			deployFromEtcdBinary(etcdBinary, hostsList)
			os.Exit(0)
		}
		if gitCommit != "" {
			deployFromGitCommit(gitCommit, hostsList)
			os.Exit(0)
		}
		if gitTag != "" {
			deployFromGitTag(gitTag, hostsList)
			os.Exit(0)
		}
	case "version":
		etcdVersion(hostsList)
	}
}
开发者ID:OnePaaS,项目名称:etcd-manager,代码行数:57,代码来源:main.go


示例18: loadEnvAgent

func loadEnvAgent() (auths []ssh.AuthMethod, err error) {
	sshAuthSock, err := net.Dial("unix", os.Getenv("SSH_AUTH_SOCK"))
	if err != nil {
		return
	}
	defer sshAuthSock.Close()
	ag := agent.NewClient(sshAuthSock)
	auths = []ssh.AuthMethod{ssh.PublicKeysCallback(ag.Signers)}
	return
}
开发者ID:colebrumley,项目名称:mssh-go,代码行数:10,代码来源:keys.go


示例19: getAgentAuth

func getAgentAuth() (auth ssh.AuthMethod, ok bool) {
	if sock := os.Getenv("SSH_AUTH_SOCK"); len(sock) > 0 {
		if agconn, err := net.Dial("unix", sock); err == nil {
			ag := agent.NewClient(agconn)
			auth = ssh.PublicKeysCallback(ag.Signers)
			ok = true
		}
	}
	return
}
开发者ID:rapidloop,项目名称:rtop-bot,代码行数:10,代码来源:sshhelper.go


示例20: Dial

// Dial creates a client connection to the given SSH server.
//
// `addr` should be provided in the following format:
//
//     [email protected]:port
//
// if `forwardAgent` is true then forwarding of the authentication agent connection will be enabled.
func Dial(addr string, socket string, forwardAgent bool) (*SSHConn, error) {
	agentConn, err := net.Dial("unix", socket)
	if err != nil {
		return nil, err
	}
	var agentOk bool
	defer func() {
		if !agentOk {
			agentConn.Close()
		}
	}()

	sshAgent := agent.NewClient(agentConn)
	signers, err := sshAgent.Signers()
	if err != nil {
		return nil, err
	}

	host, port, user, err := ParseAddr(addr)
	if err != nil {
		return nil, err
	}

	config := &ssh.ClientConfig{
		User:    user,
		Auth:    []ssh.AuthMethod{ssh.PublicKeys(signers...)},
		Timeout: ConnTimeout,
	}
	client, err := ssh.Dial("tcp", fmt.Sprintf("%s:%d", host, port), config)
	if err != nil {
		return nil, err
	}
	var clientOk bool
	defer func() {
		if !clientOk {
			client.Close()
		}
	}()

	if forwardAgent {
		if err := agent.ForwardToAgent(client, sshAgent); err != nil {
			return nil, fmt.Errorf("SetupForwardKeyring: %v", err)
		}
	}

	agentOk = true
	clientOk = true

	c := SSHConn{
		client:       client,
		agentConn:    agentConn,
		forwardAgent: forwardAgent,
	}
	return &c, nil
}
开发者ID:0xef53,项目名称:go-sshwrapper,代码行数:62,代码来源:sshwrapper.go



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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang agent.RequestAgentForwarding函数代码示例发布时间:2022-05-28
下一篇:
Golang agent.ForwardToAgent函数代码示例发布时间:2022-05-28
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap