I want to do something like the following in a script
$ ssh host1 "$(command)"
However, the sub processes gets executed before the SSH connection is established because if I do the following
$ ssh host1 "$(hostname)"
the hostname of the local machine is returned. I want to be able to use sub processes because it would make other things I am trying to do easier.
Edit: A more complete example as requested. I have a list of hostnames that I need to add crontab entries to. Each system belongs to a specific user and so should have the entry added in their name. The owner of the system is contained in the hostname of the system, so by parsing the hostname, I can su
to the appropriate user and edit their crontab accordingly. For example something like this:
for host in $(cat /tmp/hosts); do
echo -e "----------------------------"
echo -e "# SSH to host $host"
echo -e "----------------------------"
ssh $host "su - $(hostname | cut -b-5-); crontab -e";
done
ssh host1 'echo "$(hostname)"'
is a rather complicated way to sayssh host1 hostname
– thrig Feb 12 '18 at 15:24