无法在另一个命令内部运行命令。

huangapple go评论86阅读模式
英文:

Unable to run Command with another command inside

问题

抱歉,如果标题不太明确,我正在尝试在一行中使用ProxyCommand运行ssh命令:

ssh -i /home/myuser/.ssh/myprivatekey.pem ec2-user@i-00xxxxxxxxxx -o ProxyCommand="aws ssm start-session --target %h --document-name AWS-StartSSHSession --parameters 'portNumber=%p'"

上述命令可以正常工作,但我需要在Go语言中执行相同的操作。

func (s SSH) Tunnel() error {
    parts := strings.Fields(`ssh -i /home/myuser/.ssh/myprivateksy.pem ec2-user@i-00xxxxxxxxxxx`)
    parts = append(parts, `-o ProxyCommand="aws ssm start-session --target %h --document-name AWS-StartSSHSession --parameters 'portNumber=%p'"`)

    command := exec.Command(parts[0], parts[1:]...)
    command.Stderr = os.Stderr
    command.Stdout = os.Stdout
    command.Stdin = os.Stdin
    return command.Run()
}

然而,我遇到了这个错误:

zsh:1: command not found: aws ssm start-session --target i-00Xxxxxxxxxxxx --document-name AWS-StartSSHSession --parameters 'portNumber=22'

我尝试将其包含在strings.Fields中:

parts := strings.Fields(`ssh -i /home/myuser/.ssh/myprivateksy.pem ec2-user@i-00xxxxxxxxxxx -o ProxyCommand="aws ssm start-session --target %h --document-name AWS-StartSSHSession --parameters 'portNumber=%p'"`)

然而,这样做会导致另一个错误:

zsh:1: unmatched " 

我该如何实现这个目标?

谢谢。

英文:

Sorry if the title is not very self-explanatory, I am trying to run an ssh command with ProxyCommand in one line:

ssh -i /home/myuser/.ssh/myprivatekey.pem ec2-user@i-00xxxxxxxxxx -o ProxyCommand="aws ssm start-session --target %h --document-name AWS-StartSSHSession --parameters 'portNumber=%p'"

This command above works, however I need to do the same in go.

func (s SSH) Tunnel() error {
	parts := strings.Fields(`ssh -i /home/myuser/.ssh/myprivateksy.pem ec2-user@i-00xxxxxxxxxxx`)
	parts = append(parts, `-o ProxyCommand="aws ssm start-session --target %h --document-name AWS-StartSSHSession --parameters 'portNumber=%p'"`)

	command := exec.Command(parts[0], parts[1:]...)
	command.Stderr = os.Stderr
	command.Stdout = os.Stdout
	command.Stdin = os.Stdin
	return command.Run()
}

However I got this error:

zsh:1: command not found: aws ssm start-session --target i-00Xxxxxxxxxxxx --document-name AWS-StartSSHSession --parameters 'portNumber=22'

I have tried to include it in the strings.Fields:

parts := strings.Fields(`ssh -i /home/myuser/.ssh/myprivateksy.pem ec2-user@i-00xxxxxxxxxxx -o ProxyCommand="aws ssm start-session --target %h --document-name AWS-StartSSHSession --parameters 'portNumber=%p'"`)

However with this I have another error:

zsh:1: unmatched " 

How can I achieve this?

Thanks

答案1

得分: 1

根据您提供的shell输入,以下内容应该可以工作:

parts := strings.Fields(`ssh -i /home/myuser/.ssh/myprivateksy.pem ec2-user@i-00xxxxxxxxxxx -o`)
parts = append(parts, `ProxyCommand=aws ssm start-session --target %h --document-name AWS-StartSSHSession --parameters 'portNumber=%p'`)

这将将-o后面的部分作为一个不带引号的单个参数传递。Shell处理将会移除这些引号。

英文:

Based on the shell input you gave, the following should work:

parts := strings.Fields(`ssh -i /home/myuser/.ssh/myprivateksy.pem ec2-user@i-00xxxxxxxxxxx -o`)
parts = append(parts, `ProxyCommand=aws ssm start-session --target %h --document-name AWS-StartSSHSession --parameters 'portNumber=%p'`)

This will pass the part that comes after -o as a single argument without the quotes. Shell processing would remove those quotes.

huangapple
  • 本文由 发表于 2021年9月23日 10:58:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/69293487.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定