MCPcopy Create free account
hub / github.com/1Panel-dev/1Panel / NewClient

Function NewClient

core/utils/ssh/ssh.go:36–72  ·  view source on GitHub ↗
(c ConnInfo)

Source from the content-addressed store, hash-verified

34}
35
36func 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
74func (c *SSHClient) Run(shell string) (string, error) {
75 shell = c.SudoItem + " " + shell

Callers

nothing calls this directly

Calls 3

RunMethod · 0.95
DialWithTimeoutFunction · 0.85
makePrivateKeySignerFunction · 0.70

Tested by

no test coverage detected