fix: ssh formatting exception bug when executing commands (#2406)

* fix: ssh formatting exception bug when executing commands

* Update ssh_connector.go

WARNING: Use `nolint:gosec` annotation

* Update ssh_connector.go

* Update ssh_connector.go

* Update local_connector.go

* Update local_connector.go

* Update init_repository.yaml
This commit is contained in:
dbb_DingYongliang 2024-09-18 11:33:33 +08:00 committed by GitHub
parent 92dd64f227
commit e4957a648b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 7 additions and 5 deletions

View File

@ -31,7 +31,7 @@
mkdir -p /etc/apt/sources.list.d
# add repository
rm -rf /etc/apt/sources.list.d/*
echo 'deb [trusted=yes] file://tmp/kubekey/iso /' > /etc/apt/sources.list.d/kubekey.list
echo 'deb [trusted=yes] file:///tmp/kubekey/iso /' > /etc/apt/sources.list.d/kubekey.list
# update repository
apt-get update
# install

View File

@ -97,7 +97,7 @@ func (c *localConnector) ExecuteCommand(ctx context.Context, cmd string) ([]byte
klog.V(5).InfoS("exec local command", "cmd", cmd)
// find command interpreter in env. default /bin/bash
command := c.Cmd.CommandContext(ctx, "sudo", "-E", localShell, "-c", cmd)
command := c.Cmd.CommandContext(ctx, "sudo", "-E", localShell, "-c", "\"", cmd, "\"")
if c.Password != "" {
command.SetStdin(bytes.NewBufferString(c.Password + "\n"))
}

View File

@ -24,6 +24,7 @@ import (
"io"
"io/fs"
"os"
"os/exec"
"os/user"
"path/filepath"
"strings"
@ -234,7 +235,7 @@ func (c *sshConnector) FetchFile(_ context.Context, src string, dst io.Writer) e
}
// ExecuteCommand in remote host
func (c *sshConnector) ExecuteCommand(_ context.Context, cmd string) ([]byte, error) {
func (c *sshConnector) ExecuteCommand(ctx context.Context, cmd string) ([]byte, error) {
klog.V(5).InfoS("exec ssh command", "cmd", cmd, "host", c.Host)
// create ssh session
session, err := c.client.NewSession()
@ -245,7 +246,8 @@ func (c *sshConnector) ExecuteCommand(_ context.Context, cmd string) ([]byte, er
}
defer session.Close()
cmd = fmt.Sprintf("sudo -E %s -c \"%q\"", c.shell, cmd)
//nolint:gosec
command := exec.CommandContext(ctx, "sudo", "-E", c.shell, "-c", "\"", cmd, "\"")
// get pipe from session
stdin, _ := session.StdinPipe()
stdout, _ := session.StdoutPipe()
@ -255,7 +257,7 @@ func (c *sshConnector) ExecuteCommand(_ context.Context, cmd string) ([]byte, er
return nil, err
}
// Start the remote command
if err := session.Start(cmd); err != nil {
if err := session.Start(command.String()); err != nil {
return nil, err
}
if c.Password != "" {