I'm trying to run a command over SSH and want the evaluation of an expression in the command to happen on the remote machine.
I'm trying to run this:
ssh -A username@ip "sudo docker exec -it "$(docker ps | grep 'some' | awk '{ print $1 }')" python manage.py shell"
but the expression $(docker ps | grep 'some' | awk '{ print $1 }')
is not evaluated correctly on the remote machine when I use the ssh command.
To confirm, if I first ssh into the remote machine, and then run sudo docker exec -it "$(docker ps | grep 'some' | awk '{ print $1 }')" python manage.py shell
, it does evaluate correctly and gives me a shell successfully. I just cannot make it work directly from my local machine as a part of an argument to the ssh command.
What can I do to make it work as a part of the ssh command?
The problem with doing the command below is that I receive a Pseudo-terminal will not be allocated because stdin is not a terminal.
message from my terminal (iTerm) and do not get a shell like I'm expecting after the execution of this command.
ssh -A username@ip <<'EOL'
name="$(docker ps | grep 'some' | awk '{ print $1 }')"
docker exec -it $name python manage.py shell
EOL
CodePudding user response:
You need to escape all the characters that need to be interpreted by the remote shell like so:
ssh -A username@ip "sudo docker exec -it \"\$(docker ps | grep 'some' | awk '{ print \$1 }')\" python manage.py shell"
This way you will send the quotes belonging to the -it argument, as well as the $ sign unchanged and the remote shell will execute them.