Fred J. Fred J. - 1 year ago 52
Bash Question

Issue shell commands on the remote server from local machine

The following command issued on a Mac terminal is failing the

command on the remote shell.

However it works if I log in to the server and issue the command there with replacing ";" with "&&"

ssh -i "myKey.pem" user@host ‘docker stop $(docker ps -a -q --filter ancestor=name/kind); docker rm $(docker ps -a -q --filter ancestor=name/kind); docker rmi name/kind; docker build -t name/kind .; sudo docker run -it -d -p 80:80 name/kind’

Cannot connect to the Docker daemon. Is the docker daemon running on this host?

I need to run this command form the local terminal because if is part of bigger command which first build the project locally and
it to the server.

`$bigger-command && then-the-ssh-as-shown-above.

How do I go about it? Thanks

Answer Source

The best way to pass very complex commands to ssh is the create a script on the server side.

If you need to pass some parameters, proceed this way:

  • create a .sh file on your localhost
  • scp it to your remote host
  • run `ssh user@remotehost 'bash'

This should do the trick without giving you headaches about escapement.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download