Home > Net >  Unable to run Command with another command inside
Unable to run Command with another command inside

Time:09-23

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

CodePudding user response:

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.

  •  Tags:  
  • go
  • Related