Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ssh formatting exception bug when executing commands #2406

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion builtin/roles/init/init-os/tasks/init_repository.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pkg/connector/local_connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
}
Expand Down
8 changes: 5 additions & 3 deletions pkg/connector/ssh_connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"io"
"io/fs"
"os"
"os/exec"
"os/user"
"path/filepath"
"strings"
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -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 != "" {
Expand Down
Loading