StartSSH 启动ssh
()
| 34 | |
| 35 | // StartSSH 启动ssh |
| 36 | func (mySSH *SSH) start() { |
| 37 | var authPayload ssh.AuthMethod |
| 38 | var err error |
| 39 | |
| 40 | sMessage := protocol.PrepareAndDecideWhichSProtoToUpper(global.G_Component.Conn, global.G_Component.Secret, global.G_Component.UUID) |
| 41 | |
| 42 | sshResheader := &protocol.Header{ |
| 43 | Sender: global.G_Component.UUID, |
| 44 | Accepter: protocol.ADMIN_UUID, |
| 45 | MessageType: protocol.SSHRES, |
| 46 | RouteLen: uint32(len([]byte(protocol.TEMP_ROUTE))), // No need to set route when agent send mess to admin |
| 47 | Route: protocol.TEMP_ROUTE, |
| 48 | } |
| 49 | |
| 50 | sshResultheader := &protocol.Header{ |
| 51 | Sender: global.G_Component.UUID, |
| 52 | Accepter: protocol.ADMIN_UUID, |
| 53 | MessageType: protocol.SSHRESULT, |
| 54 | RouteLen: uint32(len([]byte(protocol.TEMP_ROUTE))), // No need to set route when agent send mess to admin |
| 55 | Route: protocol.TEMP_ROUTE, |
| 56 | } |
| 57 | |
| 58 | sshResSuccMess := &protocol.SSHRes{ |
| 59 | OK: 1, |
| 60 | } |
| 61 | |
| 62 | sshResFailMess := &protocol.SSHRes{ |
| 63 | OK: 0, |
| 64 | } |
| 65 | |
| 66 | defer func() { |
| 67 | if err != nil { |
| 68 | protocol.ConstructMessage(sMessage, sshResheader, sshResFailMess, false) |
| 69 | sMessage.SendMessage() |
| 70 | } |
| 71 | }() |
| 72 | |
| 73 | switch mySSH.Method { |
| 74 | case UPMETHOD: |
| 75 | authPayload = ssh.Password(mySSH.Password) |
| 76 | case CERMETHOD: |
| 77 | var key ssh.Signer |
| 78 | key, err = ssh.ParsePrivateKey(mySSH.Certificate) |
| 79 | if err != nil { |
| 80 | return |
| 81 | } |
| 82 | authPayload = ssh.PublicKeys(key) |
| 83 | } |
| 84 | |
| 85 | sshDial, err := ssh.Dial("tcp", mySSH.Addr, &ssh.ClientConfig{ |
| 86 | User: mySSH.Username, |
| 87 | Auth: []ssh.AuthMethod{authPayload}, |
| 88 | HostKeyCallback: ssh.InsecureIgnoreHostKey(), |
| 89 | Timeout: 10 * time.Second, |
| 90 | }) |
| 91 | if err != nil { |
| 92 | return |
| 93 | } |
no test coverage detected