1

I want to SSH over multiple server (host 1, host2 and host 3) and save output of multiple commands (cmd1, cmd2 etc) in file (output.properties) on local server. I know there are a few posts similar to this, but I a not sure where I am going wrong. Below is the code snippet.

folderPath="cd /usr/local/apps/tempdir";
echo $folderPath;
eval $folderPath;

# host 1
echo RepCard1=$(expr $(grep -r "sample text" * | wc -l) / 2) >> /usr/local/host1/tempdir/output.properties;

# Server 2

ssh -T user@host2 <<EOF >>/usr/local/host1/tempdir/output.properties;
#alias GetDuplicateCardStats="cd /usr/local/apps/tempdir"
folderPath="cd /usr/local/apps/tempdir";
echo $folderPath;
eval $folderPath;

echo RepCard2=$(expr $(grep -r "sample text" * | wc -l) / 2);
exit
EOF

. /usr/local/host1/tempdir/output.properties;

echo "host 1"
echo $RepCard1

echo "host 2"
echo $RepCard2
# RepCard2 is always executed on Host 1 and print value from host 1

What is the mistake in the above script?

  • It's not clear what you are asking. Please include in your post both the output you were expecting and the output you observed. As a side note, your variables RepCard1 and RepCard2 are undefined. You have not assigned any values to them. – dg99 Oct 14 '14 at 18:13
  • Hi and welcome to the site. Whenever you post something like this, also include the exact way it is failing. Are there any error messages? Is no output returned? You also need to show us exactly how you are running the script and what shell you are using. Please edit your question and include these details. – terdon Oct 14 '14 at 18:13
1

In this line:

echo RepCard2=$(expr $(grep -r "sample text" * | wc -l) / 2);

expr and grep will be run on the local system, not the SSH server. You need to escape the $ characters so they'll be kept in the here-doc and sent to the server.

echo RepCard2=\$(expr \$(grep -r "sample text" * | wc -l) / 2);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.