英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论