0

This question already has an answer here:

I have the following function :

GetHostName () {
NODE01_CHECK=`cat /etc/hosts | grep -w "node01" | awk '{print $1}'`
NODE02_CHECK=`cat /etc/hosts | grep -w "node02" | awk '{print $1}'`

IS_NODE1=`ifconfig -a | grep -w $NODE01_CHECK`
IS_NODE2=`ifconfig -a | grep -w $NODE02_CHECK`

if [[ ! -z $IS_NODE1 ]]; then
    echo "This is NODE 1"
fi

if [[ ! -z $IS_NODE2 ]]; then
    echo "This is Node 2"
fi

}

This script will identify if a certain ip is configured on one of the two nodes belonging to a cluster. This works fine locally, but I need to run it remotely from a server that only knows of the VIP of the cluster. The goal is to transfer some files to both nodes.

So when I run :

scp -r /tmp/files CLUST_VIP 
ssh CLUST_VIP <<EOF

        NODE01_CHECK=`cat /etc/hosts | grep -w "node01" | awk '{print $1}'`
        NODE02_CHECK=`cat /etc/hosts | grep -w "node02" | awk '{print $1}'`

        IS_NODE1=`ifconfig -a | grep -w $NODE01_CHECK`
        IS_NODE2=`ifconfig -a | grep -w $NODE02_CHECK`

        if [[ ! -z $IS_NODE1 ]]; then
               scp -r /tmp/files node02
        fi

        if [[ ! -z $IS_NODE2 ]]; then
               scp -r /tmp/files node01
        fi

  EOF

However, now while running the same commands in a ssh block, I get the following messages :

Usage: grep [OPTION]... PATTERN [FILE]...
Try 'grep --help' for more information.
Usage: grep [OPTION]... PATTERN [FILE]...
Try 'grep --help' for more information.
Pseudo-terminal will not be allocated because stdin is not a terminal.

I have also tried using ssh -t and that removed the above errors regarding grep, but the environment variables do not seem to work.

Is there a way to use environment variables over a ssh block?

marked as duplicate by Gilles shell-script Mar 3 '18 at 10:19

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

2

man bash:

The format of here-documents is:

          <<[-]word
                  here-document
          delimiter

No parameter and variable expansion, command substitution, arithmetic expansion, or pathname expansion is performed on word. If any characters in word are quoted, the delimiter is the result of quote removal on word, and the lines in the here-document are not expanded. If word is unquoted, all lines of the here-document are subjected to parameter expansion, command substitution, and arithmetic expansion, the character sequence \<newline> is ignored, and \ must be used to quote the characters \, $, and `.

2

Your variables and subshells are being executed prior to sending it over SSH. You can see that if you replace your ssh command with cat. You could quote EOF to prevent that expansion (as highlighted in AlexP's answer), or, since you're already doing an scp right above that step you could just put this in a script and execute it on the other side.

Since you're using bash-isms in it you might want to consider that anyway just in case this is ever executed by someone who doesn't use bash as their shell.

Also, grep can process files, so you don't need to cat a file into it, and you're missing a target on your IS_NODE2 block scp command.

  • Thanks for the suggestion. I think a secondary script on the target is the way to go in this case. Just tesded it and it works. – robertpas Dec 8 '16 at 13:44

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