douyanti2808 2015-06-09 03:00
浏览 110
已采纳

golang goroutine使用SSHAgent身份验证无法正常工作,并引发一些意外情况

I am writing a little tool for executing commands in parallel on many different hosts. All the hosts could login with a same private key. So, I want to use SSHAgent to get auth to login. When I use it for a single host, it works well. But when I use it in many goroutines, it doesn't work. I am very confused, is there somebody can help me? Thanks very much. The code used sshagent are as below:

func ExcuteRemote(uname,host,cmd string) (bool,error) {
        ip,err := GetIp(host)
        if err != nil {
                fmt.Println(err)
                return false,err
        }

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

        config := &ssh.ClientConfig{
                User: uname,
                Auth: auths,
        }
        fmt.Println("after config")

        client, err := ssh.Dial("tcp", ip+":"+"22", config)
        if err != nil {
                fmt.Println("something wrong when dial")
                fmt.Println(err)
                return false,err
        }

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

        var b bytes.Buffer
        session.Stdout = &b
        err = session.Run(cmd)
        if err != nil {
            return false,err
        }

        fmt.Println(b.String())
        return true,nil
}

And the code used channel are as below :

func testchannel(ch chan int, uname,host,cmd string) error{
        result,err := ExcuteRemote(uname,host,cmd)
        if result {
                fmt.Println(host + " ok")
        } else {
                fmt.Println(err)
                fmt.Println(host + " failed")
        }
        ch <- 1
        return nil
} 

in main:

if *list != "" {
       runtime.GOMAXPROCS(runtime.NumCPU())
       hosts,err := ReadLine(*list)
       if nil != err {
               panic(err.Error())
       }
       chs := make([]chan int, len(hosts))
       for index := 0; index < len(hosts); index++ {
               chs[index] = make(chan int)
               go testchannel(chs[index],uname,hosts[index],*cmd)
       }
       for _, ch := range(chs) {
               <-ch
       }
}

the panic like this :

panic: unreachable

goroutine 5 [running]:
golang.org/x/crypto/ssh/agent.(*client).Sign(0xc20801e440, 0x7f053381a9f8, 0xc208094100, 0xc208121b00, 0x201, 0x240, 0xc2080a6009, 0x0, 0x0)
        /usr/local/gosrc/src/golang.org/x/crypto/ssh/agent/client.go:342 +0x472
golang.org/x/crypto/ssh/agent.(*agentKeyringSigner).Sign(0xc20801e560, 0x7f053380fb38, 0xc20803c2d0, 0xc208121b00, 0x201, 0x240, 0xe, 0x0, 0x0)
        /usr/local/gosrc/src/golang.org/x/crypto/ssh/agent/client.go:562 +0x71
golang.org/x/crypto/ssh.publicKeyCallback.auth(0xc20801e480, 0xc2080a4040, 0x14, 0x20, 0xc20802a954, 0x4, 0x7f053381a998, 0xc2080ca000, 0x7f053380fb38, 0xc20803c2d0, ...)
        /usr/local/gosrc/src/golang.org/x/crypto/ssh/client_auth.go:210 +0x5ee
golang.org/x/crypto/ssh.(*connection).clientAuthenticate(0xc2080b8080, 0xc2080b2000, 0x0, 0x0)
        /usr/local/gosrc/src/golang.org/x/crypto/ssh/client_auth.go:34 +0x4e3
golang.org/x/crypto/ssh.(*connection).clientHandshake(0xc2080b8080, 0xc20801e4c0, 0x11, 0xc2080b2000, 0x0, 0x0)
        /usr/local/gosrc/src/golang.org/x/crypto/ssh/client.go:112 +0x62e
golang.org/x/crypto/ssh.NewClientConn(0x7f05338111e8, 0xc2080aa000, 0xc20801e4c0, 0x11, 0xc2080420a0, 0x0, 0x0, 0x0, 0x41be71, 0x0, ...)
        /usr/local/gosrc/src/golang.org/x/crypto/ssh/client.go:74 +0x140
golang.org/x/crypto/ssh.Dial(0x785a50, 0x3, 0xc20801e4c0, 0x11, 0xc2080420a0, 0x11, 0x0, 0x0)
        /usr/local/gosrc/src/golang.org/x/crypto/ssh/client.go:176 +0xf9
main.ExcuteRemote(0xc20802a954, 0x4, 0xc20802a9a0, 0xe, 0x7fffa171b7fd, 0x6, 0xc200000000, 0x0, 0x0)
        /home/myname/easyssh/r.go:126 +0x83f
main.testchannel(0xc208036120, 0xc20802a954, 0x4, 0xc20802a9a0, 0xe, 0x7fffa171b7fd, 0x6, 0x0, 0x0)
        /home/myname/easyssh/r.go:217 +0x8c
created by main.main
        /home/myname/gocode/easyssh/r.go:262 +0x638
  • 写回答

1条回答 默认 最新

  • dougu4704 2015-06-12 07:51
    关注

    I have solved the problem myself. As my understanding, the panic is caused by multiple goroutines want to use the connection of sshagent to the local server. Now, I only set connection once, and keep it connected until the program over. The code in function main :

        //get config
        var config *ssh.ClientConfig
        if sshAgent, err := net.Dial("unix", os.Getenv("SSH_AUTH_SOCK")); err == nil {
                config = GetConfig(uname,sshAgent)
                defer sshAgent.Close()
        } else {
                panic(err)
        }
    

    When I need a config in a goroutine, I pass the config as a param into it, it works well!

    My function in a goroutine is as follow:

    func RunInChannel(ch chan int, host,cmd string,config *ssh.ClientConfig) error{
        result,_ := ExcuteRemote(host,cmd,config)
        if result {
            fmt.Println("
    " + host + "\t\tok
    ")
        } else {
            ret,err := ExcuteRemote(host,cmd,config)
            if ret {
                fmt.Println("
    " + host + "\t\tok
    ")
            } else {
                fmt.Println(err)
                fmt.Println("
    " + host + "\t\tfailed
    ")
            }
        }
        ch <- 1
        return nil
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 软件自定义无线电该怎样使用
  • ¥15 R语言mediation包做中介分析,直接效应和间接效应都很小,为什么?
  • ¥15 Jenkins+k8s部署slave节点offline
  • ¥15 微信小游戏反编译后,出现找不到分包的情况
  • ¥15 如何实现从tello无人机上获取实时传输的视频流,然后将获取的视频通过yolov5进行检测
  • ¥15 WPF使用Canvas绘制矢量图问题
  • ¥15 用三极管设计一个单管共射放大电路
  • ¥15 孟德尔随机化r语言运行问题
  • ¥15 pyinstaller编译的时候出现No module named 'imp'
  • ¥15 nirs_kit中打码怎么看(打码文件是csv格式)