(c ConnInfo)
| 34 | } |
| 35 | |
| 36 | func NewClient(c ConnInfo) (*SSHClient, error) { |
| 37 | config := &gossh.ClientConfig{} |
| 38 | config.SetDefaults() |
| 39 | addr := net.JoinHostPort(c.Addr, fmt.Sprintf("%d", c.Port)) |
| 40 | config.User = c.User |
| 41 | if c.AuthMode == "password" { |
| 42 | config.Auth = []gossh.AuthMethod{gossh.Password(c.Password)} |
| 43 | } else { |
| 44 | signer, err := makePrivateKeySigner(c.PrivateKey, c.PassPhrase) |
| 45 | if err != nil { |
| 46 | return nil, err |
| 47 | } |
| 48 | config.Auth = []gossh.AuthMethod{gossh.PublicKeys(signer)} |
| 49 | } |
| 50 | if c.DialTimeOut == 0 { |
| 51 | c.DialTimeOut = 5 * time.Second |
| 52 | } |
| 53 | config.Timeout = c.DialTimeOut |
| 54 | |
| 55 | config.HostKeyCallback = gossh.InsecureIgnoreHostKey() |
| 56 | proto := "tcp" |
| 57 | if strings.Contains(c.Addr, ":") { |
| 58 | proto = "tcp6" |
| 59 | } |
| 60 | client, err := DialWithTimeout(proto, addr, c.UseProxy, config) |
| 61 | if nil != err { |
| 62 | return nil, err |
| 63 | } |
| 64 | sshClient := &SSHClient{Client: client} |
| 65 | if c.User == "root" { |
| 66 | return sshClient, nil |
| 67 | } |
| 68 | if _, err := sshClient.Run("sudo -n ls"); err == nil { |
| 69 | sshClient.SudoItem = "sudo" |
| 70 | } |
| 71 | return sshClient, nil |
| 72 | } |
| 73 | |
| 74 | func (c *SSHClient) Run(shell string) (string, error) { |
| 75 | shell = c.SudoItem + " " + shell |
nothing calls this directly
no test coverage detected